Skip to content

Commit 9b5fd6e

Browse files
committed
feat: use optionals to be able to tell unset styles from set ones
1 parent 48b414e commit 9b5fd6e

File tree

3 files changed

+324
-51
lines changed

3 files changed

+324
-51
lines changed

internal/config/config_test.go

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import (
55
"path/filepath"
66
"testing"
77

8+
glamourStyles "github.com/charmbracelet/glamour/styles"
9+
"github.com/charmbracelet/lipgloss"
10+
"github.com/goccy/go-yaml"
811
"github.com/spf13/viper"
12+
13+
"github.com/museslabs/kyma/internal/option"
14+
"github.com/museslabs/kyma/internal/tui/transitions"
915
)
1016

1117
func TestLoad(t *testing.T) {
@@ -234,3 +240,259 @@ func TestCreateDefaultConfig(t *testing.T) {
234240
)
235241
}
236242
}
243+
244+
func TestPrecedence(t *testing.T) {
245+
tmpDir := t.TempDir()
246+
247+
testConfig := `global:
248+
style:
249+
border: rounded
250+
border_color: "#FF0000"
251+
layout: center
252+
theme: dark
253+
254+
presets:
255+
test:
256+
style:
257+
border: hidden
258+
theme: notty
259+
border_color: "#fff"
260+
layout: center
261+
`
262+
testConfigPath := filepath.Join(tmpDir, "kyma.yaml")
263+
if err := os.WriteFile(testConfigPath, []byte(testConfig), 0644); err != nil {
264+
t.Fatalf("Failed to write test config: %v", err)
265+
}
266+
267+
if err := Load(testConfigPath); err != nil {
268+
t.Fatalf("Load() error = %v", err)
269+
}
270+
271+
tests := []struct {
272+
name string
273+
properties string
274+
want Properties
275+
}{
276+
{
277+
name: "slide properties should override global ones",
278+
properties: `style:
279+
border: hidden
280+
border_color: "#000"
281+
layout: left
282+
theme: dracula`,
283+
want: Properties{
284+
Title: "",
285+
Style: StyleConfig{
286+
Layout: option.Some(
287+
lipgloss.NewStyle().Align(lipgloss.Left, lipgloss.Left),
288+
),
289+
Border: option.Some(lipgloss.HiddenBorder()),
290+
BorderColor: "#000",
291+
Theme: option.Some(GlamourTheme{
292+
Style: *glamourStyles.DefaultStyles["dracula"],
293+
Name: "dracula",
294+
}),
295+
},
296+
Transition: transitions.Get("none", transitions.Fps),
297+
Notes: "",
298+
ImageBackend: "chafa",
299+
},
300+
},
301+
{
302+
name: "border from default styles",
303+
properties: `style:
304+
border_color: "#000"
305+
layout: left
306+
theme: dracula`,
307+
want: Properties{
308+
Title: "",
309+
Style: StyleConfig{
310+
Layout: option.Some(
311+
lipgloss.NewStyle().Align(lipgloss.Left, lipgloss.Left),
312+
),
313+
Border: option.Some(lipgloss.RoundedBorder()),
314+
BorderColor: "#000",
315+
Theme: option.Some(GlamourTheme{
316+
Style: *glamourStyles.DefaultStyles["dracula"],
317+
Name: "dracula",
318+
}),
319+
},
320+
Transition: transitions.Get("none", transitions.Fps),
321+
Notes: "",
322+
ImageBackend: "chafa",
323+
},
324+
},
325+
{
326+
name: "border color from default styles",
327+
properties: `style:
328+
border: hidden
329+
layout: left
330+
theme: dracula`,
331+
want: Properties{
332+
Title: "",
333+
Style: StyleConfig{
334+
Layout: option.Some(
335+
lipgloss.NewStyle().Align(lipgloss.Left, lipgloss.Left),
336+
),
337+
Border: option.Some(lipgloss.HiddenBorder()),
338+
BorderColor: "#FF0000",
339+
Theme: option.Some(GlamourTheme{
340+
Style: *glamourStyles.DefaultStyles["dracula"],
341+
Name: "dracula",
342+
}),
343+
},
344+
Transition: transitions.Get("none", transitions.Fps),
345+
Notes: "",
346+
ImageBackend: "chafa",
347+
},
348+
},
349+
{
350+
name: "layout from default styles",
351+
properties: `style:
352+
border: hidden
353+
border_color: "#000"
354+
theme: dracula`,
355+
want: Properties{
356+
Title: "",
357+
Style: StyleConfig{
358+
Layout: option.Some(
359+
lipgloss.NewStyle().Align(lipgloss.Center, lipgloss.Center),
360+
),
361+
Border: option.Some(lipgloss.HiddenBorder()),
362+
BorderColor: "#000",
363+
Theme: option.Some(GlamourTheme{
364+
Style: *glamourStyles.DefaultStyles["dracula"],
365+
Name: "dracula",
366+
}),
367+
},
368+
Transition: transitions.Get("none", transitions.Fps),
369+
Notes: "",
370+
ImageBackend: "chafa",
371+
},
372+
},
373+
{
374+
name: "theme from default styles",
375+
properties: `style:
376+
border: hidden
377+
border_color: "#000"
378+
layout: left`,
379+
want: Properties{
380+
Title: "",
381+
Style: StyleConfig{
382+
Layout: option.Some(
383+
lipgloss.NewStyle().Align(lipgloss.Left, lipgloss.Left),
384+
),
385+
Border: option.Some(lipgloss.HiddenBorder()),
386+
BorderColor: "#000",
387+
Theme: option.Some(GlamourTheme{
388+
Style: *glamourStyles.DefaultStyles["dark"],
389+
Name: "dark",
390+
}),
391+
},
392+
Transition: transitions.Get("none", transitions.Fps),
393+
Notes: "",
394+
ImageBackend: "chafa",
395+
},
396+
},
397+
{
398+
name: "use a preset",
399+
properties: `preset: test`,
400+
want: Properties{
401+
Title: "",
402+
Style: StyleConfig{
403+
Layout: option.Some(
404+
lipgloss.NewStyle().Align(lipgloss.Center, lipgloss.Center),
405+
),
406+
Border: option.Some(lipgloss.HiddenBorder()),
407+
BorderColor: "#fff",
408+
Theme: option.Some(GlamourTheme{
409+
Style: *glamourStyles.DefaultStyles["notty"],
410+
Name: "notty",
411+
}),
412+
},
413+
Transition: transitions.Get("none", transitions.Fps),
414+
Notes: "",
415+
ImageBackend: "chafa",
416+
},
417+
},
418+
{
419+
name: "use a preset and override border",
420+
properties: `preset: test
421+
style:
422+
border: rounded`,
423+
want: Properties{
424+
Title: "",
425+
Style: StyleConfig{
426+
Layout: option.Some(
427+
lipgloss.NewStyle().Align(lipgloss.Center, lipgloss.Center),
428+
),
429+
Border: option.Some(lipgloss.RoundedBorder()),
430+
BorderColor: "#fff",
431+
Theme: option.Some(GlamourTheme{
432+
Style: *glamourStyles.DefaultStyles["notty"],
433+
Name: "notty",
434+
}),
435+
},
436+
Transition: transitions.Get("none", transitions.Fps),
437+
Notes: "",
438+
ImageBackend: "chafa",
439+
},
440+
},
441+
}
442+
for _, tt := range tests {
443+
t.Run(tt.name, func(t *testing.T) {
444+
var p Properties
445+
if err := yaml.Unmarshal([]byte(tt.properties), &p); err != nil {
446+
t.Fatalf("yaml.Unmarshal() error = %v", err)
447+
}
448+
449+
if p.Title != tt.want.Title {
450+
t.Errorf("p.Title = %s, want = %s", p.Title, tt.want.Title)
451+
}
452+
453+
if p.Transition != tt.want.Transition {
454+
t.Errorf("p.Transition = %s, want = %s", p.Transition, tt.want.Transition)
455+
}
456+
457+
if p.Notes != tt.want.Notes {
458+
t.Errorf("p.Notes = %s, want = %s", p.Notes, tt.want.Notes)
459+
}
460+
461+
if p.ImageBackend != tt.want.ImageBackend {
462+
t.Errorf("p.ImageBackend = %s, want = %s", p.ImageBackend, tt.want.ImageBackend)
463+
}
464+
465+
if p.Style.BorderColor != tt.want.Style.BorderColor {
466+
t.Errorf(
467+
"p.Style.BorderColor = %s, want = %s",
468+
p.Style.BorderColor,
469+
tt.want.Style.BorderColor,
470+
)
471+
}
472+
473+
if p.Style.Border != tt.want.Style.Border {
474+
t.Errorf("p.Style.Border = %v, want = %v", p.Style.Border, tt.want.Style.Border)
475+
}
476+
477+
if p.Style.Theme != tt.want.Style.Theme {
478+
t.Errorf("p.Style.Theme = %v, want = %v", p.Style.Theme, tt.want.Style.Theme)
479+
}
480+
481+
if p.Style.Layout.Unwrap().
482+
GetAlignHorizontal() !=
483+
tt.want.Style.Layout.Unwrap().GetAlignHorizontal() ||
484+
p.Style.Layout.Unwrap().
485+
GetAlignVertical() !=
486+
tt.want.Style.Layout.Unwrap().
487+
GetAlignVertical() {
488+
t.Errorf(
489+
"p.Style.Layout = %f, %f, want = %f, %f",
490+
p.Style.Layout.Unwrap().GetAlignHorizontal(),
491+
p.Style.Layout.Unwrap().GetAlignVertical(),
492+
tt.want.Style.Layout.Unwrap().GetAlignHorizontal(),
493+
tt.want.Style.Layout.Unwrap().GetAlignVertical(),
494+
)
495+
}
496+
})
497+
}
498+
}

0 commit comments

Comments
 (0)