github.com/iamlucasvieira/ComTemplate@v0.0.0-20231228160247-43229117158f/pkg/cli/parser_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  var mockData = `
    10  - name: First template
    11    text: |
    12      %{title}
    13  
    14      %{body}
    15  
    16    variables:
    17      - name: title
    18      - name: body
    19  
    20  - name: Second template
    21    text: |
    22      [%{identifier}]%{title}
    23  
    24      %{body}
    25  
    26    variables:
    27      - name: identifier
    28      - name: title
    29      - name: body
    30  `
    31  
    32  func TestParse(t *testing.T) {
    33  	templates, err := parse(mockData)
    34  
    35  	if err != nil {
    36  		t.Errorf("error parsing yaml: %v", err)
    37  	}
    38  
    39  	t.Run("should have two templates", func(t *testing.T) {
    40  		if len(templates) != 2 {
    41  			t.Errorf("expected two templates, got %d", len(templates))
    42  		}
    43  	})
    44  
    45  	t.Run("should have a name", func(t *testing.T) {
    46  		if templates[0].Name != "First template" {
    47  			t.Errorf("expected name to be 'First template', got '%s'", templates[0].Name)
    48  		}
    49  	})
    50  
    51  	t.Run("should have a text", func(t *testing.T) {
    52  		if templates[0].Text != "%{title}\n\n%{body}\n" {
    53  			t.Errorf("expected text to be '%%{title}\n\n%%{body}\n', got '%s'", templates[0].Text)
    54  		}
    55  	})
    56  
    57  	t.Run("should have two variables", func(t *testing.T) {
    58  		if len(templates[0].Variables) != 2 {
    59  			t.Errorf("expected two variables, got %d", len(templates[0].Variables))
    60  		}
    61  	})
    62  
    63  }
    64  
    65  func TestOpen(t *testing.T) {
    66  	// Create a temporary file
    67  	f, err := os.CreateTemp("", "test")
    68  	defer os.Remove(f.Name())
    69  
    70  	if err != nil {
    71  		t.Errorf("error creating temp file: %v", err)
    72  	}
    73  
    74  	// Write mock data to file
    75  	_, err = f.Write([]byte(mockData))
    76  	if err != nil {
    77  		t.Fatalf("error writing to temp file: %v", err)
    78  	}
    79  
    80  	// Close the file before reopening in open function
    81  	err = f.Close()
    82  	if err != nil {
    83  		t.Fatalf("error closing file: %v", err)
    84  	}
    85  
    86  	// Open the file
    87  	data, err := open(f.Name())
    88  
    89  	if err != nil {
    90  		t.Errorf("error opening file: %v", err)
    91  	}
    92  
    93  	if data != mockData {
    94  		t.Errorf("expected data to be '%s', got '%s'", mockData, data)
    95  	}
    96  
    97  	if err != nil {
    98  		t.Errorf("error closing file: %v", err)
    99  	}
   100  }
   101  
   102  func TestRead(t *testing.T) {
   103  	// Create a temporary file
   104  	f, err := os.CreateTemp("", "test")
   105  	defer os.Remove(f.Name())
   106  
   107  	if err != nil {
   108  		t.Errorf("error creating temp file: %v", err)
   109  	}
   110  
   111  	// Write mock data to file
   112  	_, err = f.Write([]byte(mockData))
   113  	if err != nil {
   114  		t.Fatalf("error writing to temp file: %v", err)
   115  	}
   116  
   117  	// Close the file before reopening in open function
   118  	err = f.Close()
   119  	if err != nil {
   120  		t.Fatalf("error closing file: %v", err)
   121  	}
   122  
   123  	// Open the file
   124  	templates, err := read(f.Name())
   125  
   126  	if err != nil {
   127  		t.Errorf("error reading file: %v", err)
   128  	}
   129  
   130  	t.Run("should have two templates", func(t *testing.T) {
   131  		if len(templates) != 2 {
   132  			t.Errorf("expected two templates, got %d", len(templates))
   133  		}
   134  	})
   135  
   136  	t.Run("should have a name", func(t *testing.T) {
   137  		if templates[0].Name != "First template" {
   138  			t.Errorf("expected name to be 'First template', got '%s'", templates[0].Name)
   139  		}
   140  	})
   141  
   142  	t.Run("should have a text", func(t *testing.T) {
   143  		if templates[0].Text != "%{title}\n\n%{body}\n" {
   144  			t.Errorf("expected text to be '%%{title}\n\n%%{body}\n', got '%s'", templates[0].Text)
   145  		}
   146  	})
   147  
   148  	t.Run("should have two variables", func(t *testing.T) {
   149  		if len(templates[0].Variables) != 2 {
   150  			t.Errorf("expected two variables, got %d", len(templates[0].Variables))
   151  		}
   152  	})
   153  }
   154  
   155  func TestReadDefault(t *testing.T) {
   156  	// Create a temporaty directory
   157  	tempDir, err := os.MkdirTemp("", "testDir")
   158  	defer os.RemoveAll(tempDir)
   159  
   160  	if err != nil {
   161  		t.Errorf("error creating temp directory: %v", err)
   162  	}
   163  
   164  	tempFilePath := filepath.Join(tempDir, "comtemplate.yml")
   165  	f, err := os.Create(tempFilePath)
   166  
   167  	if err != nil {
   168  		t.Errorf("error creating temp file: %v", err)
   169  	}
   170  
   171  	// Write mock data to file
   172  	_, err = f.Write([]byte(mockData))
   173  	if err != nil {
   174  		t.Fatalf("error writing to temp file: %v", err)
   175  	}
   176  
   177  	// Close the file before reopening in open function
   178  	err = f.Close()
   179  
   180  	if err != nil {
   181  		t.Fatalf("error closing file: %v", err)
   182  	}
   183  
   184  	// Change the working directory to the temporary directory
   185  	err = os.Chdir(tempDir)
   186  	if err != nil {
   187  		t.Fatalf("error changing working directory: %v", err)
   188  	}
   189  
   190  	templates, err := ReadDefault()
   191  
   192  	if err != nil {
   193  		t.Errorf("error reading file: %v", err)
   194  	}
   195  
   196  	t.Run("should have two templates", func(t *testing.T) {
   197  		if len(templates) != 2 {
   198  			t.Errorf("expected two templates, got %d", len(templates))
   199  		}
   200  	})
   201  
   202  }
   203  
   204  func TestCreateDefault(t *testing.T) {
   205  	// Create a temporaty directory
   206  	tempDir, err := os.MkdirTemp("", "testDir")
   207  
   208  	if err != nil {
   209  		t.Errorf("error creating temp directory: %v", err)
   210  	}
   211  
   212  	defer os.RemoveAll(tempDir)
   213  
   214  	// Change the working directory to the temporary directory
   215  	err = os.Chdir(tempDir)
   216  
   217  	if err != nil {
   218  		t.Fatalf("error changing working directory: %v", err)
   219  	}
   220  
   221  	err = CreateDefault()
   222  
   223  	if err != nil {
   224  		t.Errorf("error creating default file: %v", err)
   225  	}
   226  
   227  	t.Run("should have created a file", func(t *testing.T) {
   228  		_, err := os.Stat("comtemplate.yml")
   229  
   230  		if err != nil {
   231  			t.Errorf("expected file to exist, got %v", err)
   232  		}
   233  	})
   234  
   235  	// Check content of file
   236  	data, err := read("comtemplate.yml")
   237  
   238  	if err != nil {
   239  		t.Errorf("error opening file: %v", err)
   240  	}
   241  
   242  	t.Run("should have default content with 2 template", func(t *testing.T) {
   243  		if len(data) != 2 {
   244  			t.Errorf("expected two templates, got %d", len(data))
   245  		}
   246  	})
   247  
   248  }
   249  
   250  func TestPopulateTemplate(t *testing.T) {
   251  	templates, err := parse(mockData)
   252  
   253  	if err != nil {
   254  		t.Errorf("error parsing yaml: %v", err)
   255  	}
   256  
   257  	variables := make(map[string]string)
   258  
   259  	variables["title"] = "Test title"
   260  	variables["body"] = "Test body"
   261  
   262  	wantString := "Test title\n\nTest body\n"
   263  
   264  	gotString, err := PopulateTemplate(templates[0], variables)
   265  
   266  	if err != nil {
   267  		t.Errorf("error populating template: %v", err)
   268  	}
   269  
   270  	if gotString != wantString {
   271  		t.Errorf("expected string to be '%s', got '%s'", wantString, gotString)
   272  	}
   273  }
   274  
   275  func TestValidateTemplate(t *testing.T) {
   276  	testCases := []struct {
   277  		id          int
   278  		description string
   279  		t           Template
   280  		want        bool
   281  	}{
   282  		{
   283  			id:          1,
   284  			description: "should return true for valid template",
   285  			t: Template{
   286  				Name: "Test",
   287  				Text: "Test %{test}",
   288  				Variables: []Variable{
   289  					{Name: "test"},
   290  				},
   291  			},
   292  			want: true,
   293  		},
   294  		{
   295  			id:          2,
   296  			description: "should return false for empty name",
   297  			t: Template{
   298  				Name: "",
   299  				Text: "Test %{test}",
   300  				Variables: []Variable{
   301  					{Name: "test"},
   302  				},
   303  			},
   304  			want: false,
   305  		},
   306  		{
   307  			id:          3,
   308  			description: "should return false for empty text",
   309  			t: Template{
   310  				Name: "Test",
   311  				Text: "",
   312  				Variables: []Variable{
   313  					{Name: "test"},
   314  				},
   315  			},
   316  			want: false,
   317  		},
   318  		{
   319  			id:          4,
   320  			description: "should return false for empty variable name",
   321  			t: Template{
   322  				Name: "Test",
   323  				Text: "Test %{test}",
   324  				Variables: []Variable{
   325  					{Name: ""},
   326  				},
   327  			},
   328  			want: false,
   329  		},
   330  		{
   331  			id:          5,
   332  			description: "should return false for variable not found in text",
   333  			t: Template{
   334  				Name: "Test",
   335  				Text: "Test %{test}",
   336  				Variables: []Variable{
   337  					{Name: "test2"},
   338  				},
   339  			},
   340  			want: false,
   341  		},
   342  		{
   343  			id:          6,
   344  			description: "should return false for invalid variable type",
   345  			t: Template{
   346  				Name: "Test",
   347  				Text: "Test %{test}",
   348  				Variables: []Variable{
   349  					{Name: "test", Type: "invalid"},
   350  				},
   351  			},
   352  		},
   353  	}
   354  
   355  	for _, tc := range testCases {
   356  		t.Run(tc.description, func(t *testing.T) {
   357  			err := tc.t.validate(tc.id)
   358  			if err != nil {
   359  				if tc.want {
   360  					t.Errorf("expected no error, got %v", err)
   361  				}
   362  			} else {
   363  				if !tc.want {
   364  					t.Errorf("expected error, got nil")
   365  				}
   366  			}
   367  		})
   368  	}
   369  }