github.com/pkalwak/bagins@v0.0.0-20210317172317-694ac5ce2f54/tagfile_test.go (about)

     1  // tagfile_test
     2  package bagins_test
     3  
     4  import (
     5  	"fmt"
     6  	"github.com/pkalwak/bagins"
     7  	"os"
     8  	"path/filepath"
     9  	"strconv"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  // Field TESTS
    15  
    16  func TestNewField(t *testing.T) {
    17  	exp_label := "test-field"
    18  	exp_value := "this is my test"
    19  	f := bagins.NewTagField(exp_label, exp_value)
    20  
    21  	if f == nil { // This should test type but for the life of me I can't figure out how.
    22  		t.Error("Tag Field object not returned")
    23  	}
    24  	if f.Label() != exp_label {
    25  		t.Error("Tag Field label not created properly!")
    26  	}
    27  	if f.Value() != exp_value {
    28  		t.Error("Tag Field value not created properly!")
    29  	}
    30  }
    31  
    32  // Using a single test for set and get labels since they rely on one another.
    33  func TestLabel(t *testing.T) {
    34  	f := bagins.NewTagField("label", "value")
    35  	if f.Label() != "label" {
    36  		t.Error("Tag Field label not created properly!")
    37  	}
    38  
    39  	f.SetLabel("new-label")
    40  	if f.Label() != "new-label" {
    41  		t.Error("Tag Field label not reset properly!")
    42  	}
    43  
    44  	if f.Value() != "value" {
    45  		t.Error("Tag Field value not set or retained properly when label changed!")
    46  	}
    47  }
    48  
    49  // Using single test for set and get values since they rely on one another.
    50  func TestValue(t *testing.T) {
    51  	f := bagins.NewTagField("label", "value")
    52  	if f.Value() != "value" {
    53  		t.Error("Tag Field value not created properly!")
    54  	}
    55  
    56  	f.SetValue("new value")
    57  	if f.Value() != "new value" {
    58  		t.Error("Tag Field value not set or read properly!")
    59  	}
    60  
    61  	if f.Label() != "label" {
    62  		t.Error("Tag Field label value not retained when value set!")
    63  	}
    64  }
    65  
    66  // FieldList TESTS
    67  
    68  func TestNewTagFieldList(t *testing.T) {
    69  	var tfl interface{} = bagins.NewTagFieldList()
    70  	if _, ok := tfl.(*bagins.TagFieldList); !ok {
    71  		t.Error("TagFieldList not returned!")
    72  	}
    73  }
    74  
    75  // Doing a unified test for Fields and SetFields
    76  func TestFields(t *testing.T) {
    77  
    78  	fl := bagins.NewTagFieldList()
    79  	test_len := func(l int) { // DRY!
    80  		if len(fl.Fields()) != l {
    81  			t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!")
    82  		}
    83  	}
    84  
    85  	test_len(0)
    86  
    87  	newFields := []bagins.TagField{
    88  		*bagins.NewTagField("label1", "value1"),
    89  		*bagins.NewTagField("label2", "value2"),
    90  		*bagins.NewTagField("label3", "value3"),
    91  	}
    92  	fl.SetFields(newFields)
    93  	test_len(3)
    94  
    95  	for i := 0; i < 3; i++ {
    96  		exp := fmt.Sprintf("label%d", i+1)
    97  		act := fl.Fields()[i].Label()
    98  		if exp != act {
    99  			t.Error("Expected", exp, "but returned", act)
   100  		}
   101  	}
   102  }
   103  
   104  func TestAddField(t *testing.T) {
   105  	fl := bagins.NewTagFieldList()
   106  	exp_len := 100
   107  	for i := 0; i < exp_len; i++ {
   108  		tmp := strconv.Itoa(i)
   109  		fl.AddField(*bagins.NewTagField(tmp, tmp))
   110  	}
   111  
   112  	if len(fl.Fields()) != exp_len {
   113  		t.Error("Expected", exp_len, "fields but returned", len(fl.Fields()), "!")
   114  	}
   115  
   116  	for i, f := range fl.Fields() {
   117  		if f.Value() != strconv.Itoa(i) {
   118  			t.Error("Expected field value of", strconv.Itoa(i), "but returned", f.Value(), "!")
   119  		}
   120  	}
   121  }
   122  
   123  func TestRemoveField(t *testing.T) {
   124  	fl := bagins.NewTagFieldList()
   125  	test_len := func(l int) { // DRY again!
   126  		if len(fl.Fields()) != l {
   127  			t.Error("Expected TagField length of", l, "but", len(fl.Fields()), "was returned!")
   128  		}
   129  	}
   130  
   131  	for i := 0; i < 100; i++ {
   132  		tmp := strconv.Itoa(i)
   133  		fl.AddField(*bagins.NewTagField(tmp, tmp))
   134  	}
   135  	test_len(100)
   136  
   137  	// Should error if removing out of range.
   138  	if err := fl.RemoveField(-6); err == nil {
   139  		t.Error("Trying to remove negative index does not produce expected error!")
   140  	}
   141  	if err := fl.RemoveField(100); err == nil {
   142  		t.Error("Trying to remove out of bound index does not produce expected error!")
   143  	}
   144  	test_len(100)
   145  
   146  	// Remove every other one of the first 25 and test
   147  	for i := 0; i < 50; i++ {
   148  		if i%2 == 0 {
   149  			fl.RemoveField(i)
   150  		}
   151  	}
   152  	test_len(75)
   153  
   154  }
   155  
   156  // TagFile TESTS
   157  
   158  func TestNewTagFile(t *testing.T) {
   159  	_, err := bagins.NewTagFile("tagfile.txt")
   160  	if err != nil {
   161  		t.Error("Tagfile raised an error incorrectly!")
   162  	}
   163  	_, err = bagins.NewTagFile(".tagfile")
   164  	if err == nil {
   165  		t.Error("Bag tagfile name did not raise error as expected.")
   166  	}
   167  }
   168  
   169  func TestReadTagFile(t *testing.T) {
   170  	// Expected Data
   171  	exp_list := [][]string{
   172  		{"description", strings.Repeat("test ", 40)},
   173  		{"title", "This is my title"},
   174  		{"description", strings.Repeat("more ", 80)},
   175  	}
   176  
   177  	// Prep the test file
   178  	testPath := filepath.Join(os.TempDir(), "_GOTEST_READTAGFILE_bagit.txt")
   179  	tagFile, _ := bagins.NewTagFile(testPath)
   180  	for _, exp := range exp_list {
   181  		tagFile.Data.AddField(*bagins.NewTagField(exp[0], exp[1]))
   182  	}
   183  	tagFile.Create()
   184  	defer os.Remove(testPath)
   185  
   186  	// Open and test parsing the file.
   187  	tf, errs := bagins.ReadTagFile(testPath)
   188  	for _, err := range errs {
   189  		t.Error(err)
   190  	}
   191  	if len(tf.Data.Fields()) != 3 {
   192  		t.Error("Expected 3 but returned", len(tf.Data.Fields()), "fields!")
   193  	}
   194  
   195  	fields := tagFile.Data.Fields()
   196  	for idx, exp := range exp_list {
   197  		if fields[idx].Label() != exp[0] {
   198  			t.Error("Tag field", idx, "label", fields[idx].Label(), "is not expected value of", exp[0])
   199  		}
   200  		if fields[idx].Value() != exp[1] {
   201  			t.Error("Tag field", idx, "value", fields[idx].Value(), "is not expected value of", exp[1])
   202  		}
   203  	}
   204  }
   205  
   206  func TestTagFileCreate(t *testing.T) {
   207  	testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt")
   208  	tagFile, _ := bagins.NewTagFile(testPath)
   209  	tagFile.Data.AddField(*bagins.NewTagField("BagIt-Version", "A metadata element MUST consist of a label, a colon, and a value, each separated by optional whitespace.  It is RECOMMENDED that lines not exceed 79 characters in length.  Long values may be continued onto the next line by inserting a newline (LF), a carriage return (CR), or carriage return plus newline (CRLF) and indenting the next line with linear white space (spaces or tabs)."))
   210  	tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encodeing", "UTF-8"))
   211  
   212  	err := tagFile.Create()
   213  	if err != nil {
   214  		t.Error(err)
   215  	}
   216  	fileInfo, err := os.Stat(testPath)
   217  	if err != nil {
   218  		t.Error("File and path", testPath, "not created!")
   219  	}
   220  	if fileInfo.Size() == 0 {
   221  		t.Error("Tag file was created but is empty.")
   222  	}
   223  	os.RemoveAll(filepath.Dir(testPath))
   224  }
   225  
   226  func TestTagFileToString(t *testing.T) {
   227  	testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt")
   228  	tagFile, _ := bagins.NewTagFile(testPath)
   229  	tagFile.Data.AddField(*bagins.NewTagField("BagIt-Version", "0.97"))
   230  	tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encoding", "UTF-8"))
   231  	tagFile.Data.AddField(*bagins.NewTagField("Long-Line", "A metadata element MUST consist of a label, a colon, and a value, each separated by optional whitespace.  It is RECOMMENDED that lines not exceed 79 characters in length.  Long values may be continued onto the next line by inserting a newline (LF), a carriage return (CR), or carriage return plus newline (CRLF) and indenting the next line with linear white space (spaces or tabs)."))
   232  
   233  	str, err := tagFile.ToString()
   234  	if err != nil {
   235  		t.Error(err)
   236  	}
   237  	expected := `BagIt-Version:  0.97
   238  Tag-File-Character-Encoding:  UTF-8
   239  Long-Line:  A metadata element MUST consist of a label, a colon, and a value,
   240      each separated by optional whitespace.  It is RECOMMENDED that lines not
   241      exceed 79 characters in length.  Long values may be continued onto the next
   242      line by inserting a newline (LF), a carriage return (CR), or carriage
   243      return plus newline (CRLF) and indenting the next line with linear white
   244      space (spaces or tabs).
   245  `
   246  	if str != expected {
   247  		t.Errorf("ToString() returned\n\n%s  \nExpected\n\n%s", str, expected)
   248  	}
   249  }