github.com/NeowayLabs/nash@v0.2.2-0.20200127205349-a227041ffd50/scanner/lex_regression_test.go (about)

     1  package scanner
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/madlambda/nash/token"
     7  )
     8  
     9  func TestLexerIssue34(t *testing.T) {
    10  	expected := []Token{
    11  		{typ: token.Ident, val: "cat"},
    12  		{typ: token.Arg, val: "/etc/passwd"},
    13  		{typ: token.Gt, val: ">"},
    14  		{typ: token.Arg, val: "/dev/null"},
    15  		{typ: token.Ident, val: "echo"},
    16  		{typ: token.String, val: "hello world"},
    17  		{typ: token.Semicolon, val: ";"},
    18  		{typ: token.EOF},
    19  	}
    20  
    21  	testTable("test-issue-34", `cat /etc/passwd > /dev/null echo "hello world"`, expected, t)
    22  }
    23  
    24  func TestLexerIssue21(t *testing.T) {
    25  	expected := []Token{
    26  		{typ: token.Ident, val: "cmd"},
    27  		{typ: token.Gt, val: ">"},
    28  		{typ: token.Variable, val: "$outFname"},
    29  		{typ: token.Semicolon, val: ";"},
    30  		{typ: token.EOF},
    31  	}
    32  
    33  	testTable("test redirection variable", `cmd > $outFname`, expected, t)
    34  }
    35  
    36  func TestLexerIssue22(t *testing.T) {
    37  	expected := []Token{
    38  		{typ: token.Fn, val: "fn"},
    39  		{typ: token.Ident, val: "gocd"},
    40  		{typ: token.LParen, val: "("},
    41  		{typ: token.Ident, val: "path"},
    42  		{typ: token.RParen, val: ")"},
    43  		{typ: token.LBrace, val: "{"},
    44  		{typ: token.If, val: "if"},
    45  		{typ: token.Variable, val: "$path"},
    46  		{typ: token.Equal, val: "=="},
    47  		{typ: token.String, val: ""},
    48  		{typ: token.LBrace, val: "{"},
    49  		{typ: token.Ident, val: "cd"},
    50  		{typ: token.Variable, val: "$GOPATH"},
    51  		{typ: token.Semicolon, val: ";"},
    52  		{typ: token.RBrace, val: "}"},
    53  		{typ: token.Else, val: "else"},
    54  		{typ: token.LBrace, val: "{"},
    55  		{typ: token.Ident, val: "cd"},
    56  		{typ: token.Variable, val: "$GOPATH"},
    57  		{typ: token.Plus, val: "+"},
    58  		{typ: token.String, val: "/src/"},
    59  		{typ: token.Plus, val: "+"},
    60  		{typ: token.Variable, val: "$path"},
    61  		{typ: token.Semicolon, val: ";"},
    62  		{typ: token.RBrace, val: "}"},
    63  		{typ: token.RBrace, val: "}"},
    64  		{typ: token.EOF},
    65  	}
    66  
    67  	testTable("test issue 22", `fn gocd(path) {
    68      if $path == "" {
    69          cd $GOPATH
    70      } else {
    71          cd $GOPATH + "/src/" + $path
    72      }
    73  }`, expected, t)
    74  }
    75  
    76  func TestLexerIssue19(t *testing.T) {
    77  	line := `version = "4.5.6"
    78  canonName <= echo -n $version | sed "s/\\.//g"`
    79  
    80  	expected := []Token{
    81  		{typ: token.Ident, val: "version"},
    82  		{typ: token.Assign, val: "="},
    83  		{typ: token.String, val: "4.5.6"},
    84  		{typ: token.Semicolon, val: ";"},
    85  		{typ: token.Ident, val: "canonName"},
    86  		{typ: token.AssignCmd, val: "<="},
    87  		{typ: token.Ident, val: "echo"},
    88  		{typ: token.Arg, val: "-n"},
    89  		{typ: token.Variable, val: "$version"},
    90  		{typ: token.Pipe, val: "|"},
    91  		{typ: token.Ident, val: "sed"},
    92  		{typ: token.String, val: "s/\\.//g"},
    93  		{typ: token.Semicolon, val: ";"},
    94  		{typ: token.EOF}}
    95  
    96  	testTable("test issue 19", line, expected, t)
    97  }
    98  
    99  func TestLexerIssue38(t *testing.T) {
   100  	expected := []Token{
   101  		{typ: token.Ident, val: "cd"},
   102  		{typ: token.LParen, val: "("},
   103  		{typ: token.Variable, val: "$GOPATH"},
   104  		{typ: token.Plus, val: "+"},
   105  		{typ: token.String, val: "/src/"},
   106  		{typ: token.Plus, val: "+"},
   107  		{typ: token.Variable, val: "$path"},
   108  		{typ: token.RParen, val: ")"},
   109  		{typ: token.Semicolon, val: ";"},
   110  		{typ: token.EOF},
   111  	}
   112  
   113  	testTable("test issue38", `cd($GOPATH + "/src/" + $path)`, expected, t)
   114  }
   115  
   116  func TestLexerIssue43(t *testing.T) {
   117  	expected := []Token{
   118  		{typ: token.Fn, val: "fn"},
   119  		{typ: token.Ident, val: "gpull"},
   120  		{typ: token.LParen, val: "("},
   121  		{typ: token.RParen, val: ")"},
   122  		{typ: token.LBrace, val: "{"},
   123  		{typ: token.Ident, val: "branch"},
   124  		{typ: token.AssignCmd, val: "<="},
   125  		{typ: token.Ident, val: "git"},
   126  		{typ: token.Arg, val: "rev-parse"},
   127  		{typ: token.Arg, val: "--abbrev-ref"},
   128  		{typ: token.Ident, val: "HEAD"},
   129  		{typ: token.Pipe, val: "|"},
   130  		{typ: token.Ident, val: "xargs"},
   131  		{typ: token.Ident, val: "echo"},
   132  		{typ: token.Arg, val: "-n"},
   133  		{typ: token.Semicolon, val: ";"},
   134  		{typ: token.Ident, val: "git"},
   135  		{typ: token.Ident, val: "pull"},
   136  		{typ: token.Ident, val: "origin"},
   137  		{typ: token.Variable, val: "$branch"},
   138  		{typ: token.Semicolon, val: ";"},
   139  		{typ: token.Ident, val: "refreshPrompt"},
   140  		{typ: token.LParen, val: "("},
   141  		{typ: token.RParen, val: ")"},
   142  		{typ: token.Semicolon, val: ";"},
   143  		{typ: token.RBrace, val: "}"},
   144  		{typ: token.EOF},
   145  	}
   146  
   147  	testTable("test issue #41", `fn gpull() {
   148          branch <= git rev-parse --abbrev-ref HEAD | xargs echo -n
   149  
   150          git pull origin $branch
   151          refreshPrompt()
   152  }`, expected, t)
   153  }
   154  
   155  func TestLexerIssue68(t *testing.T) {
   156  	expected := []Token{
   157  		{typ: token.Ident, val: "cat"},
   158  		{typ: token.Ident, val: "PKGBUILD"},
   159  		{typ: token.Pipe, val: "|"},
   160  		{typ: token.Ident, val: "sed"},
   161  		{typ: token.String, val: "s#\\\\$pkgdir#/home/i4k/alt#g"},
   162  		{typ: token.Gt, val: ">"},
   163  		{typ: token.Ident, val: "PKGBUILD2"},
   164  		{typ: token.Semicolon, val: ";"},
   165  		{typ: token.EOF},
   166  	}
   167  
   168  	testTable("test issue #68", `cat PKGBUILD | sed "s#\\\\$pkgdir#/home/i4k/alt#g" > PKGBUILD2`, expected, t)
   169  }
   170  
   171  func TestLexerIssue85(t *testing.T) {
   172  	expected := []Token{
   173  		{typ: token.Ident, val: "a"},
   174  		{typ: token.AssignCmd, val: "<="},
   175  		{typ: token.Arg, val: "-echo"},
   176  		{typ: token.Ident, val: "hello"},
   177  		{typ: token.Semicolon, val: ";"},
   178  		{typ: token.EOF},
   179  	}
   180  
   181  	testTable("test issue 85", `a <= -echo hello`, expected, t)
   182  }
   183  
   184  func TestLexerIssue69(t *testing.T) {
   185  	expected := []Token{
   186  		{typ: token.Ident, val: "a"},
   187  		{typ: token.Assign, val: "="},
   188  		{typ: token.LParen, val: "("},
   189  		{typ: token.Variable, val: "$a"},
   190  		{typ: token.Plus, val: "+"},
   191  		{typ: token.String, val: "b"},
   192  		{typ: token.RParen, val: ")"},
   193  		{typ: token.Semicolon, val: ";"},
   194  		{typ: token.EOF},
   195  	}
   196  
   197  	testTable("test69", `a = ($a + "b")`, expected, t)
   198  
   199  }
   200  
   201  func TestLexerIssue127(t *testing.T) {
   202  	expected := []Token{
   203  		{typ: token.Ident, val: "rm"},
   204  		{typ: token.Arg, val: "-rf"},
   205  		{typ: token.Illegal, val: "test127:1:12: Unrecognized character in action: U+002F '/'"},
   206  		{typ: token.EOF},
   207  	}
   208  
   209  	testTable("test127", `rm -rf $HOME/.vim`, expected, t)
   210  }