File: struct_map_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (76 lines) | stat: -rw-r--r-- 2,189 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package vals

import (
	"testing"

	"src.elv.sh/pkg/persistent/hash"
)

type testStructMap struct {
	Name  string
	Score float64
}

func (testStructMap) IsStructMap() {}

func (m testStructMap) ScorePlusTen() float64 { return m.Score + 10 }

// Equivalent to testStructMap for Elvish.
type testStructMap2 struct {
	Name         string
	Score        float64
	ScorePlusTen float64
}

func (testStructMap2) IsStructMap() {}

func TestStructMap(t *testing.T) {
	TestValue(t, testStructMap{"ls", 1.0}).
		Kind("map").
		Bool(true).
		Hash(
			hash.DJB(Hash("name"), Hash("ls"))+
				hash.DJB(Hash("score"), Hash(1.0))+
				hash.DJB(Hash("score-plus-ten"), Hash(11.0))).
		Repr(`[&name=ls &score=(num 1.0) &score-plus-ten=(num 11.0)]`).
		Len(3).
		Equal(
			// Struct maps behave like maps, so they are equal to normal maps
			// and other struct maps with the same entries.
			MakeMap("name", "ls", "score", 1.0, "score-plus-ten", 11.0),
			testStructMap{"ls", 1.0},
			testStructMap2{"ls", 1.0, 11.0}).
		NotEqual("a", MakeMap(), testStructMap{"ls", 2.0}, testStructMap{"l", 1.0}).
		HasKey("name", "score", "score-plus-ten").
		HasNoKey("bad", 1.0).
		IndexError("bad", NoSuchKey("bad")).
		IndexError(1.0, NoSuchKey(1.0)).
		AllKeys("name", "score", "score-plus-ten").
		Index("name", "ls").
		Index("score", 1.0).
		Index("score-plus-ten", 11.0)
}

type testPseudoMap struct{}

func (testPseudoMap) Kind() string      { return "test-pseudo-map" }
func (testPseudoMap) Fields() StructMap { return testStructMap{"pseudo", 100} }

func TestPseudoMap(t *testing.T) {
	TestValue(t, testPseudoMap{}).
		Repr("[^test-pseudo-map &name=pseudo &score=(num 100.0) &score-plus-ten=(num 110.0)]").
		HasKey("name", "score", "score-plus-ten").
		NotEqual(
			// Pseudo struct maps are nominally typed, so they are not equal to
			// maps or struct maps with the same entries.
			MakeMap("name", "", "score", 1.0, "score-plus-ten", 11.0),
			testStructMap{"ls", 1.0},
		).
		HasNoKey("bad", 1.0).
		IndexError("bad", NoSuchKey("bad")).
		IndexError(1.0, NoSuchKey(1.0)).
		AllKeys("name", "score", "score-plus-ten").
		Index("name", "pseudo").
		Index("score", 100.0).
		Index("score-plus-ten", 110.0)
}