github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/builder/dockerfile/support_test.go (about)

     1  package dockerfile
     2  
     3  import "testing"
     4  
     5  type testCase struct {
     6  	name       string
     7  	args       []string
     8  	attributes map[string]bool
     9  	expected   []string
    10  }
    11  
    12  func initTestCases() []testCase {
    13  	testCases := []testCase{}
    14  
    15  	testCases = append(testCases, testCase{
    16  		name:       "empty args",
    17  		args:       []string{},
    18  		attributes: make(map[string]bool),
    19  		expected:   []string{},
    20  	})
    21  
    22  	jsonAttributes := make(map[string]bool)
    23  	jsonAttributes["json"] = true
    24  
    25  	testCases = append(testCases, testCase{
    26  		name:       "json attribute with one element",
    27  		args:       []string{"foo"},
    28  		attributes: jsonAttributes,
    29  		expected:   []string{"foo"},
    30  	})
    31  
    32  	testCases = append(testCases, testCase{
    33  		name:       "json attribute with two elements",
    34  		args:       []string{"foo", "bar"},
    35  		attributes: jsonAttributes,
    36  		expected:   []string{"foo", "bar"},
    37  	})
    38  
    39  	testCases = append(testCases, testCase{
    40  		name:       "no attributes",
    41  		args:       []string{"foo", "bar"},
    42  		attributes: nil,
    43  		expected:   []string{"foo bar"},
    44  	})
    45  
    46  	return testCases
    47  }
    48  
    49  func TestHandleJSONArgs(t *testing.T) {
    50  	testCases := initTestCases()
    51  
    52  	for _, test := range testCases {
    53  		arguments := handleJSONArgs(test.args, test.attributes)
    54  
    55  		if len(arguments) != len(test.expected) {
    56  			t.Fatalf("In test \"%s\": length of returned slice is incorrect. Expected: %d, got: %d", test.name, len(test.expected), len(arguments))
    57  		}
    58  
    59  		for i := range test.expected {
    60  			if arguments[i] != test.expected[i] {
    61  				t.Fatalf("In test \"%s\": element as position %d is incorrect. Expected: %s, got: %s", test.name, i, test.expected[i], arguments[i])
    62  			}
    63  		}
    64  	}
    65  }