File: bits_test.go

package info (click to toggle)
rclone 1.65.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 38,352 kB
  • sloc: sh: 1,056; xml: 857; python: 693; javascript: 612; makefile: 289; ansic: 101; php: 74
file content (151 lines) | stat: -rw-r--r-- 3,345 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package fs

import (
	"encoding/json"
	"fmt"
	"strconv"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type bits = Bits[bitsChoices]

const (
	bitA bits = 1 << iota
	bitB
	bitC
)

type bitsChoices struct{}

func (bitsChoices) Choices() []BitsChoicesInfo {
	return []BitsChoicesInfo{
		{uint64(0), "OFF"},
		{uint64(bitA), "A"},
		{uint64(bitB), "B"},
		{uint64(bitC), "C"},
	}
}

// Check it satisfies the interfaces
var (
	_ flagger   = (*bits)(nil)
	_ flaggerNP = bits(0)
)

func TestBitsString(t *testing.T) {
	assert.Equal(t, "OFF", bits(0).String())
	assert.Equal(t, "A", (bitA).String())
	assert.Equal(t, "A,B", (bitA | bitB).String())
	assert.Equal(t, "A,B,C", (bitA | bitB | bitC).String())
	assert.Equal(t, "A,Unknown-0x8000", (bitA | bits(0x8000)).String())
}

func TestBitsHelp(t *testing.T) {
	assert.Equal(t, "OFF, A, B, C", bits(0).Help())
}

func TestBitsSet(t *testing.T) {
	for _, test := range []struct {
		in      string
		want    bits
		wantErr string
	}{
		{"", bits(0), ""},
		{"B", bitB, ""},
		{"B,A", bitB | bitA, ""},
		{"a,b,C", bitA | bitB | bitC, ""},
		{"A,B,unknown,E", 0, `invalid choice "unknown" from: OFF, A, B, C`},
	} {
		f := bits(0xffffffffffffffff)
		initial := f
		err := f.Set(test.in)
		if err != nil {
			if test.wantErr == "" {
				t.Errorf("Got an error when not expecting one on %q: %v", test.in, err)
			} else {
				assert.Contains(t, err.Error(), test.wantErr)
			}
			assert.Equal(t, initial, f, test.want)
		} else {
			if test.wantErr != "" {
				t.Errorf("Got no error when expecting one on %q", test.in)
			} else {
				assert.Equal(t, test.want, f)
			}
		}

	}
}

func TestBitsIsSet(t *testing.T) {
	b := bitA | bitB
	assert.True(t, b.IsSet(bitA))
	assert.True(t, b.IsSet(bitB))
	assert.True(t, b.IsSet(bitA|bitB))
	assert.False(t, b.IsSet(bitC))
	assert.False(t, b.IsSet(bitA|bitC))
}

func TestBitsType(t *testing.T) {
	f := bits(0)
	assert.Equal(t, "Bits", f.Type())
}

func TestBitsScan(t *testing.T) {
	var v bits
	n, err := fmt.Sscan(" C,B ", &v)
	require.NoError(t, err)
	assert.Equal(t, 1, n)
	assert.Equal(t, bitC|bitB, v)
}

func TestBitsUnmarshallJSON(t *testing.T) {
	for _, test := range []struct {
		in      string
		want    bits
		wantErr string
	}{
		{`""`, bits(0), ""},
		{`"B"`, bitB, ""},
		{`"B,A"`, bitB | bitA, ""},
		{`"A,B,C"`, bitA | bitB | bitC, ""},
		{`"A,B,unknown,E"`, 0, `invalid choice "unknown" from: OFF, A, B, C`},
		{`0`, bits(0), ""},
		{strconv.Itoa(int(bitB)), bitB, ""},
		{strconv.Itoa(int(bitB | bitA)), bitB | bitA, ""},
	} {
		f := bits(0xffffffffffffffff)
		initial := f
		err := json.Unmarshal([]byte(test.in), &f)
		if err != nil {
			if test.wantErr == "" {
				t.Errorf("Got an error when not expecting one on %q: %v", test.in, err)
			} else {
				assert.Contains(t, err.Error(), test.wantErr)
			}
			assert.Equal(t, initial, f, test.want)
		} else {
			if test.wantErr != "" {
				t.Errorf("Got no error when expecting one on %q", test.in)
			} else {
				assert.Equal(t, test.want, f)
			}
		}
	}
}
func TestBitsMarshalJSON(t *testing.T) {
	for _, test := range []struct {
		in   bits
		want string
	}{
		{bitA | bitC, `"A,C"`},
		{0, `"OFF"`},
	} {
		got, err := json.Marshal(&test.in)
		require.NoError(t, err)
		assert.Equal(t, test.want, string(got), fmt.Sprintf("%#v", test.in))
	}
}