| 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 |
"flag" |
| 40 |
"testing" |
| 41 |
|
| 42 |
"github.com/stretchr/testify/assert" |
| 43 |
"github.com/stretchr/testify/require" |
| 44 |
) |
| 45 |
|
| 46 |
func TestNewConfig(t *testing.T) { |
| 47 |
assert.Equal(t, Config{ |
| 48 |
Colors: true, |
| 49 |
Hyperlinks: true, |
| 50 |
}, NewConfig()) |
| 51 |
} |
| 52 |
|
| 53 |
func TestConfig_AddFlags(t *testing.T) { |
| 54 |
// set to true |
| 55 |
var fset = flag.NewFlagSet("x", flag.ContinueOnError) |
| 56 |
var conf = NewConfig() |
| 57 |
conf.AddFlags(fset, "testing.") |
| 58 |
var err = fset.Parse([]string{ |
| 59 |
"-testing.colors", |
| 60 |
"-testing.hyperlinks", |
| 61 |
}) |
| 62 |
require.NoError(t, err) |
| 63 |
assert.True(t, conf.Colors) |
| 64 |
assert.True(t, conf.Hyperlinks) |
| 65 |
// set to true & false |
| 66 |
fset = flag.NewFlagSet("x", flag.ContinueOnError) |
| 67 |
conf.AddFlags(fset, "testing.") |
| 68 |
err = fset.Parse([]string{ |
| 69 |
"-testing.colors=t", |
| 70 |
"-testing.hyperlinks=f", |
| 71 |
}) |
| 72 |
require.NoError(t, err) |
| 73 |
assert.True(t, conf.Colors) |
| 74 |
assert.False(t, conf.Hyperlinks) |
| 75 |
// set to false & true |
| 76 |
fset = flag.NewFlagSet("x", flag.ContinueOnError) |
| 77 |
conf.AddFlags(fset, "testing.") |
| 78 |
err = fset.Parse([]string{ |
| 79 |
"-testing.colors=f", |
| 80 |
"-testing.hyperlinks=t", |
| 81 |
}) |
| 82 |
require.NoError(t, err) |
| 83 |
assert.False(t, conf.Colors) |
| 84 |
assert.True(t, conf.Hyperlinks) |
| 85 |
// set to false |
| 86 |
fset = flag.NewFlagSet("x", flag.ContinueOnError) |
| 87 |
conf.AddFlags(fset, "testing.") |
| 88 |
err = fset.Parse([]string{ |
| 89 |
"-testing.colors=f", |
| 90 |
"-testing.hyperlinks=f", |
| 91 |
}) |
| 92 |
require.NoError(t, err) |
| 93 |
assert.False(t, conf.Colors) |
| 94 |
assert.False(t, conf.Hyperlinks) |
| 95 |
} |
| 96 |
|
| 97 |
func TestConfig_Apply(t *testing.T) { |
| 98 |
var conf = NewConfig() |
| 99 |
conf.Apply(WithColors(false), WithHyperlinks(false)) |
| 100 |
assert.Equal(t, Config{ |
| 101 |
Colors: false, |
| 102 |
Hyperlinks: false, |
| 103 |
}, conf) |
| 104 |
} |
| 105 |
|
| 106 |
func TestConfig_Options(t *testing.T) { |
| 107 |
var ( |
| 108 |
c1 = NewConfig() |
| 109 |
c2 Config |
| 110 |
) |
| 111 |
c2.Apply(c1.Options()...) |
| 112 |
assert.Equal(t, Config{ |
| 113 |
Colors: true, |
| 114 |
Hyperlinks: true, |
| 115 |
}, c2) |
| 116 |
} |
| 117 |
|
| 118 |
func TestConfig_colorConfig(t *testing.T) { |
| 119 |
var conf = NewConfig() |
| 120 |
assert.Equal(t, colorPin|hyperlinksPin, conf.colorConfig()) |
| 121 |
conf.Colors = false |
| 122 |
assert.Equal(t, hyperlinksPin, conf.colorConfig()) |
| 123 |
conf.Hyperlinks = false |
| 124 |
assert.Equal(t, colorConfig(0), conf.colorConfig()) |
| 125 |
} |
| 126 |
|
| 127 |
func TestWithColors(t *testing.T) { |
| 128 |
var conf Config |
| 129 |
// turn to true |
| 130 |
conf.Apply(WithColors(true)) |
| 131 |
assert.Equal(t, Config{ |
| 132 |
Colors: true, |
| 133 |
Hyperlinks: false, |
| 134 |
}, conf) |
| 135 |
// turn to false |
| 136 |
conf.Apply(WithColors(false)) |
| 137 |
assert.Equal(t, Config{ |
| 138 |
Colors: false, |
| 139 |
Hyperlinks: false, |
| 140 |
}, conf) |
| 141 |
} |
| 142 |
|
| 143 |
func TestWithHyperlinks(t *testing.T) { |
| 144 |
var conf Config |
| 145 |
// turn to true |
| 146 |
conf.Apply(WithHyperlinks(true)) |
| 147 |
assert.Equal(t, Config{ |
| 148 |
Colors: false, |
| 149 |
Hyperlinks: true, |
| 150 |
}, conf) |
| 151 |
// turn to false |
| 152 |
conf.Apply(WithHyperlinks(false)) |
| 153 |
assert.Equal(t, Config{ |
| 154 |
Colors: false, |
| 155 |
Hyperlinks: false, |
| 156 |
}, conf) |
| 157 |
} |