1package main23import (4 "net/url"5 "reflect"6 "testing"7)89func Test_resolveColor(t *testing.T) {10 tests := []struct {11 name string12 color string13 want string14 }{15 // Named colors (shields.io)16 {name: "blue", color: "blue", want: "007ec6"},17 {name: "red", color: "red", want: "e05d44"},18 {name: "green", color: "green", want: "97ca00"},19 {name: "brightgreen", color: "brightgreen", want: "44cc11"},20 {name: "yellowgreen", color: "yellowgreen", want: "a4a61d"},21 {name: "orange", color: "orange", want: "fe7d37"},22 {name: "yellow", color: "yellow", want: "dfb317"},23 {name: "lightgrey", color: "lightgrey", want: "9f9f9f"},24 {name: "lightgray", color: "lightgray", want: "9f9f9f"},25 {name: "blueviolet", color: "blueviolet", want: "8a2be2"},26 // Semantic aliases27 {name: "success", color: "success", want: "44cc11"},28 {name: "critical", color: "critical", want: "e05d44"},29 {name: "informational", color: "informational", want: "007ec6"},30 // CSS colors31 {name: "white", color: "white", want: "ffffff"},32 {name: "black", color: "black", want: "000000"},33 {name: "navy", color: "navy", want: "000080"},34 {name: "teal", color: "teal", want: "008080"},35 // Case insensitivity36 {name: "BLUE uppercase", color: "BLUE", want: "007ec6"},37 {name: "Blue mixed case", color: "Blue", want: "007ec6"},38 // Hex codes (3 digit)39 {name: "hex 3 digit", color: "fff", want: "fff"},40 {name: "hex 3 digit mixed", color: "a1b", want: "a1b"},41 // Hex codes (6 digit)42 {name: "hex 6 digit", color: "abcdef", want: "abcdef"},43 {name: "hex 6 digit uppercase", color: "ABCDEF", want: "abcdef"},44 // Hex codes (4 digit with alpha)45 {name: "hex 4 digit", color: "fffa", want: "fffa"},46 // Hex codes (8 digit with alpha)47 {name: "hex 8 digit", color: "abcdef12", want: "abcdef12"},48 // Invalid inputs49 {name: "invalid name", color: "notacolor", want: ""},50 {name: "invalid hex too short", color: "ab", want: ""},51 {name: "invalid hex too long", color: "abcdefghi", want: ""},52 {name: "invalid hex with special chars", color: "abc#ef", want: ""},53 {name: "empty string", color: "", want: ""},54 }5556 for _, tt := range tests {57 t.Run(tt.name, func(t *testing.T) {58 if got := resolveColor(tt.color); got != tt.want {59 t.Errorf("resolveColor(%q) = %q, want %q", tt.color, got, tt.want)60 }61 })62 }63}6465func Test_formatCount(t *testing.T) {66 type args struct {67 count float6468 }69 tests := []struct {70 name string71 args args72 want string73 }{74 {75 name: "",76 args: args{77 count: 100,78 },79 want: "100",80 },81 {82 name: "",83 args: args{84 count: 1000,85 },86 want: "1.0k",87 },88 {89 name: "",90 args: args{91 count: 2500,92 },93 want: "2.5k",94 },95 {96 name: "",97 args: args{98 count: 436465,99 },100 want: "436k",101 },102 {103 name: "",104 args: args{105 count: 263804,106 },107 want: "263k",108 },109 {110 name: "",111 args: args{112 count: 86400,113 },114 want: "86k",115 },116 {117 name: "",118 args: args{119 count: 81.99825581739397,120 },121 want: "82",122 },123 }124 for _, tt := range tests {125 t.Run(tt.name, func(t *testing.T) {126 if got := formatCount(tt.args.count); got != tt.want {127 t.Errorf("formatCount() = %v, want %v", got, tt.want)128 }129 })130 }131}132133func Test_processPath(t *testing.T) {134 type args struct {135 path string136 }137 tests := []struct {138 name string139 args args140 want location141 wantErr bool142 }{143 {144 name: "",145 args: args{146 path: "/github/boyter/really-cheap-chatbot/",147 },148 want: location{149 Provider: "github",150 User: "boyter",151 Repo: "really-cheap-chatbot",152 },153 wantErr: false,154 },155 {156 name: "",157 args: args{158 path: "github/boyter/really-cheap-chatbot",159 },160 want: location{161 Provider: "github",162 User: "boyter",163 Repo: "really-cheap-chatbot",164 },165 wantErr: false,166 },167 }168 for _, tt := range tests {169 t.Run(tt.name, func(t *testing.T) {170 got, err := processUrlPath(tt.args.path)171 if (err != nil) != tt.wantErr {172 t.Errorf("processUrlPath() error = %v, wantErr %v", err, tt.wantErr)173 return174 }175 if !reflect.DeepEqual(got, tt.want) {176 t.Errorf("processUrlPath() got = %v, want %v", got, tt.want)177 }178 })179 }180}181func Test_parseBadgeSettings(t *testing.T) {182183 defaultSettings := &badgeSettings{184 FontColor: "fff",185 TextShadowColor: "010101",186 TopShadowAccentColor: "bbb",187 TitleBackgroundColor: "555",188 BadgeBackgroundColor: "4c1",189 }190191 tests := []struct {192 name string193 values url.Values194 want *badgeSettings195 }{196 {197 name: "default settings",198 values: url.Values{},199 want: defaultSettings,200 },201 {202 name: "valid custom settings",203 values: url.Values{204 "font-color": []string{"abcdef"},205 "font-shadow-color": []string{"def"},206 "top-shadow-accent-color": []string{"321def"},207 "title-bg-color": []string{"456"},208 "badge-bg-color": []string{"789"},209 },210 want: &badgeSettings{211 FontColor: "abcdef",212 TextShadowColor: "def",213 TopShadowAccentColor: "321def",214 TitleBackgroundColor: "456",215 BadgeBackgroundColor: "789",216 },217 },218 {219 name: "partially-valid custom settings",220 values: url.Values{221 "font-color": []string{"123321"},222 "font-shadow-color": []string{"invalid"},223 "top-shadow-accent-color": []string{"5a534332"},224 "title-bg-color": []string{"dd"},225 "badge-bg-color": []string{"X&^%^#$^$@%20"},226 },227 want: &badgeSettings{228 FontColor: "123321",229 TextShadowColor: defaultSettings.TextShadowColor,230 TopShadowAccentColor: "5a534332",231 TitleBackgroundColor: defaultSettings.TitleBackgroundColor,232 BadgeBackgroundColor: defaultSettings.BadgeBackgroundColor,233 },234 },235 {236 name: "invalid custom settings",237 values: url.Values{238 "font-color": []string{"invalid"},239 "font-shadow-color": []string{"invalid"},240 "top-shadow-accent-color": []string{"invalid"},241 "title-bg-color": []string{"invalid"},242 "badge-bg-color": []string{"invalid"},243 },244 want: defaultSettings,245 },246 {247 name: "named colors - shields.io style",248 values: url.Values{249 "font-color": []string{"white"},250 "font-shadow-color": []string{"black"},251 "top-shadow-accent-color": []string{"lightgrey"},252 "title-bg-color": []string{"blue"},253 "badge-bg-color": []string{"brightgreen"},254 },255 want: &badgeSettings{256 FontColor: "ffffff",257 TextShadowColor: "000000",258 TopShadowAccentColor: "9f9f9f",259 TitleBackgroundColor: "007ec6",260 BadgeBackgroundColor: "44cc11",261 },262 },263 {264 name: "mixed named colors and hex codes",265 values: url.Values{266 "font-color": []string{"fff"},267 "font-shadow-color": []string{"navy"},268 "top-shadow-accent-color": []string{"bbb"},269 "title-bg-color": []string{"blueviolet"},270 "badge-bg-color": []string{"success"},271 },272 want: &badgeSettings{273 FontColor: "fff",274 TextShadowColor: "000080",275 TopShadowAccentColor: "bbb",276 TitleBackgroundColor: "8a2be2",277 BadgeBackgroundColor: "44cc11",278 },279 },280 {281 name: "case insensitive named colors",282 values: url.Values{283 "font-color": []string{"WHITE"},284 "title-bg-color": []string{"Blue"},285 "badge-bg-color": []string{"BrightGreen"},286 },287 want: &badgeSettings{288 FontColor: "ffffff",289 TextShadowColor: defaultSettings.TextShadowColor,290 TopShadowAccentColor: defaultSettings.TopShadowAccentColor,291 TitleBackgroundColor: "007ec6",292 BadgeBackgroundColor: "44cc11",293 },294 },295 }296297 for _, tt := range tests {298 t.Run(tt.name, func(t *testing.T) {299 if got := parseBadgeSettings(tt.values); !reflect.DeepEqual(got, tt.want) {300 t.Errorf("parseBadgeSettings() = %v, want %v", got, tt.want)301 }302 })303 }304}
Findings
✓ No findings reported for this file.