aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
blob: 1492e7ab357444d4b20d52536c079987d451f952 (plain)
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
// SPDX-FileCopyrightText: © 2022 Grégoire Duchêne <gduchene@awhk.org>
// SPDX-License-Identifier: ISC

package core_test

import (
	"flag"
	"strconv"
	"testing"

	"go.awhk.org/core"
)

func TestFlagT(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	fl := core.FlagT(fs, "test", 42, "", strconv.Atoi)
	t.AssertEqual(42, *fl)
	t.AssertErrorIs(nil, fs.Parse([]string{"-test=84"}))
	t.AssertEqual(84, *fl)
}

func TestFlagTVar(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	var fl int
	core.FlagTVar(fs, &fl, "test", 42, "", strconv.Atoi)
	t.AssertEqual(42, fl)
	t.AssertErrorIs(nil, fs.Parse([]string{"-test=84"}))
	t.AssertEqual(84, fl)
}

func TestFlagTSlice(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	fl := core.FlagTSlice(fs, "test", []int{42}, "", strconv.Atoi)
	t.AssertEqual([]int{42}, *fl)
	t.AssertErrorIs(nil, fs.Parse([]string{"-test=1", "-test=2", "-test=42"}))
	t.AssertEqual([]int{1, 2, 42}, *fl)
}

func TestFlagTSliceVar(s *testing.T) {
	t := core.T{T: s}

	fs := flag.NewFlagSet("", flag.PanicOnError)
	var fl []int
	core.FlagTSliceVar(fs, &fl, "test", []int{42}, "", strconv.Atoi)
	t.AssertEqual([]int{42}, fl)
	t.AssertErrorIs(nil, fs.Parse([]string{"-test=1", "-test=2", "-test=42"}))
	t.AssertEqual([]int{1, 2, 42}, fl)
}