github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/ed/ed_test.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"testing"
    11  )
    12  
    13  var (
    14  	testdata = `To be fair, this is just random weirdo stuff going on.
    15  We learn something new every day.`
    16  )
    17  
    18  // TestEDCOmmandsNonInput tests ed without commands which invoke input mode.
    19  // Input mode requires a different, more complex test setup to work.
    20  func TestEdCommandsNonInput(t *testing.T) {
    21  	for _, tt := range []struct {
    22  		name     string
    23  		cmd      string
    24  		prompt   string
    25  		suppress bool
    26  		wantOut  string
    27  	}{
    28  		{
    29  			name:    "CmdErr_Only_On",
    30  			cmd:     "H\nq\n",
    31  			wantOut: "exit\n",
    32  		},
    33  		{
    34  			name:    "CmdErr_On_Off",
    35  			cmd:     "H\nH\nq\n",
    36  			wantOut: "exit\n",
    37  		},
    38  		{
    39  			name:    "CmdErr_printErr",
    40  			cmd:     "h\nq\n",
    41  			wantOut: "exit\nexit\n",
    42  		},
    43  		{
    44  			name:    "cmdDelete_undo",
    45  			cmd:     "-1 d\nu\nq\n",
    46  			wantOut: "exit\n",
    47  		},
    48  		{
    49  			name:    "cmdDelete_invalidAddr",
    50  			cmd:     "4 d\nu\nq\n",
    51  			wantOut: "line is out of bounds\nexit\n",
    52  		},
    53  		{
    54  			name:    "CmdPrint_List",
    55  			cmd:     "-1 l\nq\n",
    56  			wantOut: "To be fair, this is just random weirdo stuff going on.$\nexit\n",
    57  		},
    58  		{
    59  			name:    "CmdPrint_Print_previous_line",
    60  			cmd:     "-1 p\nq\n",
    61  			wantOut: "To be fair, this is just random weirdo stuff going on.\nexit\n",
    62  		},
    63  		{
    64  			name:    "CmdPrint_Print_addressed line",
    65  			cmd:     "-1 n\nq\n",
    66  			wantOut: "1\tTo be fair, this is just random weirdo stuff going on.\nexit\n",
    67  		},
    68  		{
    69  			name:    "CmdPrint_InvalidAddr",
    70  			cmd:     "4 l\nq\n",
    71  			wantOut: "line is out of bounds\nexit\n",
    72  		},
    73  		{
    74  			name:    "CmdWrite_simple",
    75  			cmd:     "w\nq\n",
    76  			wantOut: "exit\n",
    77  		},
    78  		{
    79  			name:    "CmdWrite_simple_invalidAddr",
    80  			cmd:     "4 w\nq\n",
    81  			wantOut: "line is out of bounds\nexit\n",
    82  		},
    83  		{
    84  			name:    "CmdWrite_Quit",
    85  			cmd:     "wq\nq\n",
    86  			wantOut: "exit\n",
    87  		},
    88  		{
    89  			name:    "CmdWrite_W",
    90  			cmd:     "W\nq\n",
    91  			wantOut: "exit\n",
    92  		},
    93  		{
    94  			name:    "CmdMark_simple",
    95  			cmd:     "k 0\nq\n",
    96  			wantOut: "exit\n",
    97  		},
    98  		{
    99  			name:    "CmdMark_No_mark_character_supplied",
   100  			cmd:     "-1 k\nq\n",
   101  			wantOut: "no mark character supplied\nexit\n",
   102  		},
   103  		{
   104  			name:    "cmdLine_PrintLine",
   105  			cmd:     "- =\np\nq\n", // This equals to the input of `- =`[Enter press] `p` [Enter press]
   106  			wantOut: "1\nWe learn something new every day.\nexit\n",
   107  		},
   108  		{
   109  			name:    "cmdFile_setFile_default",
   110  			cmd:     "f\nq\n",
   111  			wantOut: "exit\n",
   112  		},
   113  		{
   114  			name:    "CmdMove_Move_simple",
   115  			cmd:     "1 m 2\nu\nq\n",
   116  			wantOut: "exit\n",
   117  		},
   118  		{
   119  			name:    "CmdCopy_Cut",
   120  			cmd:     "y\nq\n",
   121  			wantOut: "exit\n",
   122  		},
   123  		{
   124  			name:    "CmdScroll",
   125  			cmd:     "-1 z\nu\nq\n",
   126  			wantOut: "To be fair, this is just random weirdo stuff going on.\nWe learn something new every day.\nexit\n",
   127  		},
   128  		{
   129  			name:    "CmdScroll_invalidAddr",
   130  			cmd:     "4 z\nu\nq\n",
   131  			wantOut: "line is out of bounds\nexit\n",
   132  		},
   133  		{
   134  			name:    "CmdScroll_invalid_windowsize",
   135  			cmd:     "-1 z 0\nu\nq\n",
   136  			wantOut: "invalid window size:  0\nexit\n",
   137  		},
   138  		{
   139  			name:    "CmdSub_You_We_in_line2_1_p",
   140  			cmd:     "2 s/(We)/You/p\nu\nq\n",
   141  			wantOut: "You learn something new every day.\nexit\n",
   142  		},
   143  		{
   144  			name:    "CmdSub_You_We_in_line2_2_l",
   145  			cmd:     "2 s/(We)/You/l\nu\nq\n",
   146  			wantOut: "You learn something new every day.$\nexit\n",
   147  		},
   148  		{
   149  			name:    "CmdSub_You_We_in_line2_3_n",
   150  			cmd:     "2 s/(We)/You/n\nu\nq\n",
   151  			wantOut: "1\tYou learn something new every day.\nexit\n",
   152  		},
   153  		{
   154  			name:    "CmdSub_You_We_in_line2_3_g",
   155  			cmd:     "2 s/(We)/You/g\np\nu\nq\n",
   156  			wantOut: "You learn something new every day.\nexit\n",
   157  		},
   158  		{
   159  			name:    "CmdSub_invalidAddr",
   160  			cmd:     "4 s/(We)/You/n\nu\nq\n",
   161  			wantOut: "line is out of bounds\nexit\n",
   162  		},
   163  		{
   164  			name:    "CmdQuit_Buffer_dirty",
   165  			cmd:     "2 s/(We)/You/n\nq\nu\nq",
   166  			wantOut: "1\tYou learn something new every day.\nwarning: file modified\nexit\n",
   167  		},
   168  		{
   169  			name:    "CmdEdit_undo",
   170  			cmd:     "e\nu\nq\n",
   171  			wantOut: "87\nexit\n",
   172  		},
   173  		{
   174  			name:    "CmdPaste",
   175  			cmd:     "x\nq\n",
   176  			wantOut: "exit\n",
   177  		},
   178  		{
   179  			name:    "CmdJoin",
   180  			cmd:     "-1,2 j\np\nu\nq\n",
   181  			wantOut: "To be fair, this is just random weirdo stuff going on.We learn something new every day.\nexit\n",
   182  		},
   183  		{
   184  			name:    "CmdJoin_invalidAddr",
   185  			cmd:     "-3 j\nu\nq\n",
   186  			wantOut: "line is out of bounds\nexit\n",
   187  		},
   188  		{
   189  			name:    "CmdDump",
   190  			cmd:     "D\nq\n",
   191  			wantOut: "&{[] [To be fair, this is just random weirdo stuff going on. We learn something new every day.] [0 1] [] [0 1] false false false false 1 0 1 map[]}\nexit\n",
   192  		},
   193  	} {
   194  		t.Run("Command:"+tt.cmd, func(t *testing.T) {
   195  			tmpDir := t.TempDir()
   196  			tmpFile, err := os.CreateTemp(tmpDir, "testfile-")
   197  			if err != nil {
   198  				t.Errorf("os.CreateTemp(tmpdir, `testfile-`)=file, %q, want file, nil", err)
   199  			}
   200  			var in, out bytes.Buffer
   201  			in.WriteString(tt.cmd)
   202  			tmpFile.WriteString(testdata)
   203  			if err := runEd(&in, &out, tt.suppress, tt.prompt, tmpFile.Name()); err != nil {
   204  				t.Errorf(`runEd(&in, &out, tt.suppress, tt.prompt, "")=%q, want nil`, err)
   205  			}
   206  			if out.String() != tt.wantOut {
   207  				t.Errorf("%s failed. Got: %s, Want: %s", tt.cmd, out.String(), tt.wantOut)
   208  			}
   209  		})
   210  	}
   211  }