-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum_test.go
64 lines (51 loc) · 2.75 KB
/
checksum_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2015 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package dflag_test
import (
"flag"
"testing"
"time"
"fortio.org/assert"
"fortio.org/dflag"
)
func TestChecksumFlagSet_Differs(t *testing.T) {
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dflag.DynDuration(set, "some_duration_1", 5*time.Second, "Use it or lose it")
dflag.DynInt64(set, "some_int_1", 13371337, "Use it or lose it")
set.String("static_string_1", "foobar", "meh")
preInitChecksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("pre init checksum: %x", preInitChecksum)
set.Parse([]string{"--some_duration_1", "3s", "--static_string_1", "goodbar"})
postInitChecksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("post init checksum: %x", postInitChecksum)
assert.NotEqual(t, preInitChecksum, postInitChecksum, "checksum must be different init changed 2 flags")
assert.NoError(t, set.Set("some_int_1", "1337"))
postSet1Checksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("post set1 checksum: %x", postSet1Checksum)
assert.NotEqual(t, postInitChecksum, postSet1Checksum, "checksum must be different after a internal flag change")
assert.NoError(t, set.Set("some_duration_1", "4s"))
postSet2Checksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("post set2 checksum: %x", postSet2Checksum)
assert.NotEqual(t, postSet1Checksum, postSet2Checksum, "checksum must be different after a internal flag change")
assert.NoError(t, set.Set("some_duration_1", "3s"))
postSet3Checksum := dflag.ChecksumFlagSet(set, nil)
t.Logf("post set3 checksum: %x", postSet3Checksum)
assert.EqualValues(t, postSet1Checksum, postSet3Checksum, "flipping back duration flag to state at set1 should make it equal")
}
func TestChecksumFlagSet_Filters(t *testing.T) {
filterOnlyDuration := func(f *flag.Flag) bool { return f.Name == "some_duration_1" }
set := flag.NewFlagSet("foobar", flag.ContinueOnError)
dflag.DynDuration(set, "some_duration_1", 5*time.Second, "Use it or lose it")
dflag.DynInt64(set, "some_int_1", 13371337, "Use it or lose it")
set.Parse([]string{"--some_duration_1", "3s", "--some_int_1", "1337"})
postInitChecksum := dflag.ChecksumFlagSet(set, filterOnlyDuration)
t.Logf("post init checksum: %x", postInitChecksum)
assert.NoError(t, set.Set("some_int_1", "1337"))
postSet1Checksum := dflag.ChecksumFlagSet(set, filterOnlyDuration)
t.Logf("post set1 checksum: %x", postSet1Checksum)
assert.EqualValues(t, postInitChecksum, postSet1Checksum, "checksum should not include some_int_1 change")
assert.NoError(t, set.Set("some_duration_1", "10s"))
postSet2Checksum := dflag.ChecksumFlagSet(set, filterOnlyDuration)
t.Logf("post set2 checksum: %x", postSet2Checksum)
assert.NotEqual(t, postSet1Checksum, postSet2Checksum, "checksum change when some_duration_1 changes")
}