github.com/mitchellh/packer@v1.3.2/common/bootcommand/boot_command_ast_test.go (about)

     1  package bootcommand
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_parse(t *testing.T) {
    12  	in := "<wait><wait20><wait3s><wait4m2ns>"
    13  	in += "foo/bar > one 界"
    14  	in += "<fon> b<fOff>"
    15  	in += "<foo><f3><f12><spacebar><leftalt><rightshift><rightsuper>"
    16  	expected := []string{
    17  		"Wait<1s>",
    18  		"Wait<20s>",
    19  		"Wait<3s>",
    20  		"Wait<4m0.000000002s>",
    21  		"LIT-Press(f)",
    22  		"LIT-Press(o)",
    23  		"LIT-Press(o)",
    24  		"LIT-Press(/)",
    25  		"LIT-Press(b)",
    26  		"LIT-Press(a)",
    27  		"LIT-Press(r)",
    28  		"LIT-Press( )",
    29  		"LIT-Press(>)",
    30  		"LIT-Press( )",
    31  		"LIT-Press(o)",
    32  		"LIT-Press(n)",
    33  		"LIT-Press(e)",
    34  		"LIT-Press( )",
    35  		"LIT-Press(界)",
    36  		"LIT-On(f)",
    37  		"LIT-Press( )",
    38  		"LIT-Press(b)",
    39  		"LIT-Off(f)",
    40  		"LIT-Press(<)",
    41  		"LIT-Press(f)",
    42  		"LIT-Press(o)",
    43  		"LIT-Press(o)",
    44  		"LIT-Press(>)",
    45  		"Spec-Press(f3)",
    46  		"Spec-Press(f12)",
    47  		"Spec-Press(spacebar)",
    48  		"Spec-Press(leftalt)",
    49  		"Spec-Press(rightshift)",
    50  		"Spec-Press(rightsuper)",
    51  	}
    52  
    53  	seq, err := GenerateExpressionSequence(in)
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	for i, exp := range seq {
    58  		assert.Equal(t, expected[i], fmt.Sprintf("%s", exp))
    59  		log.Printf("%s\n", exp)
    60  	}
    61  }
    62  
    63  func Test_special(t *testing.T) {
    64  	var specials = []struct {
    65  		in  string
    66  		out string
    67  	}{
    68  		{
    69  			"<rightShift><rightshift><RIGHTSHIFT>",
    70  			"Spec-Press(rightshift)",
    71  		},
    72  		{
    73  			"<delon><delON><deLoN><DELON>",
    74  			"Spec-On(del)",
    75  		},
    76  		{
    77  			"<enteroff><enterOFF><eNtErOfF><ENTEROFF>",
    78  			"Spec-Off(enter)",
    79  		},
    80  	}
    81  	for _, tt := range specials {
    82  		seq, err := GenerateExpressionSequence(tt.in)
    83  		if err != nil {
    84  			log.Fatal(err)
    85  		}
    86  		for _, exp := range seq {
    87  			assert.Equal(t, tt.out, exp.(*specialExpression).String())
    88  		}
    89  	}
    90  }
    91  
    92  func Test_validation(t *testing.T) {
    93  	var expressions = []struct {
    94  		in    string
    95  		valid bool
    96  	}{
    97  		{
    98  			"<wait1m>",
    99  			true,
   100  		},
   101  		{
   102  			"<wait-1m>",
   103  			false,
   104  		},
   105  		{
   106  			"<f1>",
   107  			true,
   108  		},
   109  		{
   110  			"<",
   111  			true,
   112  		},
   113  	}
   114  	for _, tt := range expressions {
   115  		exp, err := GenerateExpressionSequence(tt.in)
   116  		if err != nil {
   117  			log.Fatal(err)
   118  		}
   119  
   120  		assert.Len(t, exp, 1)
   121  		err = exp[0].Validate()
   122  		if tt.valid {
   123  			assert.NoError(t, err)
   124  		} else {
   125  			assert.Error(t, err)
   126  		}
   127  	}
   128  }
   129  
   130  func Test_empty(t *testing.T) {
   131  	exp, err := GenerateExpressionSequence("")
   132  	assert.NoError(t, err, "should have parsed an empty input okay.")
   133  	assert.Len(t, exp, 0)
   134  }