appearance_test.go (3135B)
1 package appicon 2 3 import ( 4 "encoding/json" 5 "errors" 6 "reflect" 7 "testing" 8 ) 9 10 func TestFillWritesTheShapeItNames(t *testing.T) { 11 for _, tt := range []struct { 12 name string 13 fill Fill 14 want string 15 }{ 16 {"a fill the system provides", NamedFill("automatic"), `"automatic"`}, 17 {"one colour", SolidFill("srgb:1.00000,0.25279,1.00000,1.00000"), `{"solid":"srgb:1.00000,0.25279,1.00000,1.00000"}`}, 18 { 19 name: "a gradient", 20 fill: GradientFill("display-p3:0.90000,0.90000,0.90000,0.83000", "srgb:1.00000,1.00000,1.00000,0.41987"), 21 want: `{"linear-gradient":["display-p3:0.90000,0.90000,0.90000,0.83000","srgb:1.00000,1.00000,1.00000,0.41987"]}`, 22 }, 23 // A gradient wins over the other two, so a fill given more than one 24 // is written the way it is documented to be. 25 {"a gradient beside a colour", Fill{Gradient: []string{"srgb:0,0,0,1"}, Solid: "srgb:1,1,1,1"}, `{"linear-gradient":["srgb:0,0,0,1"]}`}, 26 } { 27 t.Run(tt.name, func(t *testing.T) { 28 got, err := json.Marshal(tt.fill) 29 if err != nil { 30 t.Fatalf("writing: %v", err) 31 } 32 if string(got) != tt.want { 33 t.Errorf("wrote %s, want %s", got, tt.want) 34 } 35 }) 36 } 37 } 38 39 func TestFillRejectsNamingNothing(t *testing.T) { 40 if _, err := json.Marshal(Fill{}); !errors.Is(err, ErrEmptyFill) { 41 t.Errorf("writing an empty fill returned %v, want ErrEmptyFill", err) 42 } 43 } 44 45 func TestFillReadsBackWhatItWrote(t *testing.T) { 46 for _, want := range []Fill{ 47 NamedFill("system-dark"), 48 SolidFill("srgb:1,0,0,1"), 49 GradientFill("srgb:1,1,1,1", "srgb:0,0,0,1"), 50 } { 51 data, err := json.Marshal(want) 52 if err != nil { 53 t.Fatalf("writing %v: %v", want, err) 54 } 55 var got Fill 56 if err := json.Unmarshal(data, &got); err != nil { 57 t.Fatalf("reading %s: %v", data, err) 58 } 59 if !reflect.DeepEqual(got, want) { 60 t.Errorf("read %v from %s, want %v", got, data, want) 61 } 62 } 63 } 64 65 func TestSpecializedWritesTheAppearanceOnlyWhenItHasOne(t *testing.T) { 66 data, err := json.Marshal([]Specialized[Fill]{ 67 {Value: NamedFill("system-light")}, 68 {Appearance: AppearanceDark, Value: NamedFill("system-dark")}, 69 }) 70 if err != nil { 71 t.Fatalf("writing: %v", err) 72 } 73 want := `[{"value":"system-light"},{"appearance":"dark","value":"system-dark"}]` 74 if string(data) != want { 75 t.Errorf("wrote %s, want %s", data, want) 76 } 77 } 78 79 func TestForFallsBackToTheDefault(t *testing.T) { 80 values := []Specialized[string]{ 81 {Value: "normal"}, 82 {Appearance: AppearanceTinted, Value: "lighten"}, 83 } 84 for _, tt := range []struct { 85 appearance Appearance 86 want string 87 }{ 88 {AppearanceDefault, "normal"}, 89 {AppearanceTinted, "lighten"}, 90 {AppearanceDark, "normal"}, 91 } { 92 got, ok := For(values, tt.appearance) 93 if !ok || got != tt.want { 94 t.Errorf("For(%q) = %q, %v; want %q, true", tt.appearance, got, ok, tt.want) 95 } 96 } 97 if _, ok := For[string](nil, AppearanceDark); ok { 98 t.Error("For on nothing reported a value") 99 } 100 // Without a default there is nothing to fall back to. 101 only := []Specialized[string]{{Appearance: AppearanceTinted, Value: "lighten"}} 102 if _, ok := For(only, AppearanceDark); ok { 103 t.Error("For fell back to an appearance that is not the default") 104 } 105 }