github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/cmds/elvish/eval/vals/struct_test.go (about) 1 package vals 2 3 import ( 4 "testing" 5 ) 6 7 var ( 8 testStructDescriptor = NewStructDescriptor("foo", "bar") 9 testStruct = NewStruct(testStructDescriptor, []interface{}{"lorem", "ipsum"}) 10 testStruct2 = NewStruct(testStructDescriptor, []interface{}{"lorem", "dolor"}) 11 ) 12 13 func TestStructMethods(t *testing.T) { 14 if l := testStruct.Len(); l != 2 { 15 t.Errorf("testStruct.Len() = %d, want 2", l) 16 } 17 if foo, ok := testStruct.Index("foo"); foo != "lorem" { 18 t.Errorf(`testStruct.Index("foo") = %q, want "lorem"`, foo) 19 } else if !ok { 20 t.Errorf(`testStruct.Index("foo") => false, want true`) 21 } 22 if testStruct.Equal(testStruct2) { 23 t.Errorf(`testStruct.Equal(testStruct2) => true, want false`) 24 } 25 if s2, err := testStruct.Assoc("bar", "dolor"); !Equal(s2, testStruct2) { 26 t.Errorf(`testStruct.Assoc(...) => %v, want %v`, s2, testStruct2) 27 } else if err != nil { 28 t.Errorf(`testStruct.Assoc(...) => error %s, want no error`, err) 29 } 30 wantRepr := "[&foo=lorem &bar=ipsum]" 31 if gotRepr := testStruct.Repr(NoPretty); gotRepr != wantRepr { 32 t.Errorf(`testStruct.Repr() => %q, want %q`, gotRepr, wantRepr) 33 } 34 wantJSON := `{"foo":"lorem","bar":"ipsum"}` 35 gotJSONBytes, err := testStruct.MarshalJSON() 36 gotJSON := string(gotJSONBytes) 37 if err != nil { 38 t.Errorf(`testStruct.MarshalJSON() => error %v`, err) 39 } 40 if wantJSON != gotJSON { 41 t.Errorf(`testStruct.MarshalJSON() => %q, want %q`, gotJSON, wantJSON) 42 } 43 }