|
| 1 | +// Copyright (c) 2023-2024, R.I. Pienaar and the Choria Project contributors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package forms |
| 6 | + |
| 7 | +import ( |
| 8 | + "github.com/jedib0t/go-pretty/v6/text" |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = Describe("ColorMarkup", func() { |
| 14 | + Describe("colorMarkup function", func() { |
| 15 | + It("should handle no color markup", func() { |
| 16 | + input := "Hello World" |
| 17 | + expected := "Hello World" |
| 18 | + result := colorMarkup(input) |
| 19 | + Expect(result).To(Equal(expected)) |
| 20 | + }) |
| 21 | + |
| 22 | + It("should handle single color tag", func() { |
| 23 | + input := "{red}Hello{/red} World" |
| 24 | + expected := text.Colors{text.FgRed}.Sprint("Hello") + " World" |
| 25 | + result := colorMarkup(input) |
| 26 | + Expect(result).To(Equal(expected)) |
| 27 | + }) |
| 28 | + |
| 29 | + It("should handle multiple color tags", func() { |
| 30 | + input := "{red}Hello{/red} {blue}World{/blue}" |
| 31 | + expected := text.Colors{text.FgRed}.Sprint("Hello") + " " + text.Colors{text.FgBlue}.Sprint("World") |
| 32 | + result := colorMarkup(input) |
| 33 | + Expect(result).To(Equal(expected)) |
| 34 | + }) |
| 35 | + |
| 36 | + It("should handle nested color tags", func() { |
| 37 | + input := "{red}Outer {green}Inner{/green} Text{/red}" |
| 38 | + expected := text.Colors{text.FgRed}.Sprint("Outer " + text.Colors{text.FgGreen}.Sprint("Inner") + " Text") |
| 39 | + result := colorMarkup(input) |
| 40 | + Expect(result).To(Equal(expected)) |
| 41 | + }) |
| 42 | + |
| 43 | + It("should handle case insensitive colors", func() { |
| 44 | + input := "{RED}Hello{/RED} {Blue}World{/Blue}" |
| 45 | + expected := text.Colors{text.FgRed}.Sprint("Hello") + " " + text.Colors{text.FgBlue}.Sprint("World") |
| 46 | + result := colorMarkup(input) |
| 47 | + Expect(result).To(Equal(expected)) |
| 48 | + }) |
| 49 | + |
| 50 | + It("should handle high intensity colors", func() { |
| 51 | + input := "{hired}Error{/hired} {higreen}Success{/higreen}" |
| 52 | + expected := text.Colors{text.FgHiRed}.Sprint("Error") + " " + text.Colors{text.FgHiGreen}.Sprint("Success") |
| 53 | + result := colorMarkup(input) |
| 54 | + Expect(result).To(Equal(expected)) |
| 55 | + }) |
| 56 | + |
| 57 | + It("should remove invalid color tags", func() { |
| 58 | + input := "{invalid}Text{/invalid}" |
| 59 | + expected := "Text" |
| 60 | + result := colorMarkup(input) |
| 61 | + Expect(result).To(Equal(expected)) |
| 62 | + }) |
| 63 | + |
| 64 | + It("should handle mixed valid and invalid colors", func() { |
| 65 | + input := "{red}Valid{/red} {invalid}Invalid{/invalid} {blue}Another{/blue}" |
| 66 | + expected := text.Colors{text.FgRed}.Sprint("Valid") + " Invalid " + text.Colors{text.FgBlue}.Sprint("Another") |
| 67 | + result := colorMarkup(input) |
| 68 | + Expect(result).To(Equal(expected)) |
| 69 | + }) |
| 70 | + |
| 71 | + It("should handle empty color tag", func() { |
| 72 | + input := "{red}{/red}" |
| 73 | + expected := text.Colors{text.FgRed}.Sprint("") |
| 74 | + result := colorMarkup(input) |
| 75 | + Expect(result).To(Equal(expected)) |
| 76 | + }) |
| 77 | + |
| 78 | + It("should handle all standard colors", func() { |
| 79 | + input := "{black}black{/black} {red}red{/red} {green}green{/green} {yellow}yellow{/yellow} {blue}blue{/blue} {magenta}magenta{/magenta} {cyan}cyan{/cyan} {white}white{/white}" |
| 80 | + expected := text.Colors{text.FgBlack}.Sprint("black") + " " + |
| 81 | + text.Colors{text.FgRed}.Sprint("red") + " " + |
| 82 | + text.Colors{text.FgGreen}.Sprint("green") + " " + |
| 83 | + text.Colors{text.FgYellow}.Sprint("yellow") + " " + |
| 84 | + text.Colors{text.FgBlue}.Sprint("blue") + " " + |
| 85 | + text.Colors{text.FgMagenta}.Sprint("magenta") + " " + |
| 86 | + text.Colors{text.FgCyan}.Sprint("cyan") + " " + |
| 87 | + text.Colors{text.FgWhite}.Sprint("white") |
| 88 | + result := colorMarkup(input) |
| 89 | + Expect(result).To(Equal(expected)) |
| 90 | + }) |
| 91 | + |
| 92 | + It("should handle complex nesting and preserve all text content", func() { |
| 93 | + input := "{red}Start {blue}Middle {green}End{/green} More{/blue} Final{/red}" |
| 94 | + result := colorMarkup(input) |
| 95 | + |
| 96 | + // The function should process innermost tags first |
| 97 | + // This is a complex case that tests the iterative processing |
| 98 | + Expect(result).To(ContainSubstring("Start")) |
| 99 | + Expect(result).To(ContainSubstring("Middle")) |
| 100 | + Expect(result).To(ContainSubstring("End")) |
| 101 | + Expect(result).To(ContainSubstring("More")) |
| 102 | + Expect(result).To(ContainSubstring("Final")) |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments