Debian Codegraph @

sprintf_test.go

raw
1 //
2 // Copyright (c) 2016-2022 The Aurora Authors. All rights reserved.
3 // This program is free software. It comes without any warranty,
4 // to the extent permitted by applicable law. You can redistribute
5 // it and/or modify it under the terms of the Unlicense. See LICENSE
6 // file for more details or see below.
7 //
8
9 //
10 // This is free and unencumbered software released into the public domain.
11 //
12 // Anyone is free to copy, modify, publish, use, compile, sell, or
13 // distribute this software, either in source code form or as a compiled
14 // binary, for any purpose, commercial or non-commercial, and by any
15 // means.
16 //
17 // In jurisdictions that recognize copyright laws, the author or authors
18 // of this software dedicate any and all copyright interest in the
19 // software to the public domain. We make this dedication for the benefit
20 // of the public at large and to the detriment of our heirs and
21 // successors. We intend this dedication to be an overt act of
22 // relinquishment in perpetuity of all present and future rights to this
23 // software under copyright law.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 // OTHER DEALINGS IN THE SOFTWARE.
32 //
33 // For more information, please refer to <http://unlicense.org/>
34 //
35
36 package aurora
37
38 import (
39 "testing"
40
41 "github.com/stretchr/testify/assert"
42 )
43
44 type noString string
45
46 func Test_Sprintf(t *testing.T) {
47 var want, got string
48
49 want = "delta: +3"
50 got = Sprintf(noString("delta: +%d"), 3)
51 assert.Equal(t, want, got)
52
53 want = "\033[31mdeltas: +3, 5, 9 points\033[0m"
54 got = Sprintf(Red("deltas: +%d, %d, %d points"), 3, 5, 9)
55 assert.Equal(t, want, got)
56
57 // %s
58 want = `quoted:  "blue"` +
59 `,  green`
60 got = Sprintf(Red("quoted: % 10q, % 10s"), Blue("blue"), Green("green"))
61 assert.Equal(t, want, got)
62
63 // on string
64 want = `quoted: blue, green`
65 got = Sprintf("quoted: %s, %s", Blue("blue"), Green("green"))
66 assert.Equal(t, want, got)
67
68 // no tail
69 want = `quoted: blue, green`
70 got = Sprintf(Clear("quoted: %s, %s"), Blue("blue"), Green("green"))
71 assert.Equal(t, want, got)
72
73 // precision
74 want = `value: 2.78`
75 got = Sprintf(Red("value: %1.2f"), Blue(2.7834))
76 assert.Equal(t, want, got)
77
78 // wide verb
79 want = `%!世(float64=+2.78)`
80 got = Sprintf(Red("%+1.3世"), Blue(2.7834))
81 assert.Equal(t, want, got)
82
83 // decolor
84 var au = New(WithColors(false), WithHyperlinks(false))
85 want = `+2.783`
86 got = au.Sprintf(Red("%+1.3f"), Blue(2.7834))
87 assert.Equal(t, want, got)
88 }