func TestNewTagFile(t *testing.T) { _, err := bagins.NewTagFile("tagfile.txt") if err != nil { t.Error("Tagfile raised an error incorrectly!") } _, err = bagins.NewTagFile(".tagfile") if err == nil { t.Error("Bag tagfile name did not raise error as expected.") } }
func TestTagFileCreate(t *testing.T) { testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt") tagFile, _ := bagins.NewTagFile(testPath) 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).")) tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encodeing", "UTF-8")) err := tagFile.Create() if err != nil { t.Error(err) } fileInfo, err := os.Stat(testPath) if err != nil { t.Error("File and path", testPath, "not created!") } if fileInfo.Size() == 0 { t.Error("Tag file was created but is empty.") } os.RemoveAll(filepath.Dir(testPath)) }
func TestTagFileToString(t *testing.T) { testPath := filepath.Join(os.TempDir(), "golang_test_tagfiles/_GOTEST_bagit.txt") tagFile, _ := bagins.NewTagFile(testPath) tagFile.Data.AddField(*bagins.NewTagField("BagIt-Version", "0.97")) tagFile.Data.AddField(*bagins.NewTagField("Tag-File-Character-Encoding", "UTF-8")) 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).")) str, err := tagFile.ToString() if err != nil { t.Error(err) } expected := `BagIt-Version: 0.97 Tag-File-Character-Encoding: UTF-8 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). ` if str != expected { t.Errorf("ToString() returned\n\n%s \nExpected\n\n%s", str, expected) } }
func TestReadTagFile(t *testing.T) { // Expected Data exp_list := [][]string{ []string{"description", strings.Repeat("test ", 40)}, []string{"title", "This is my title"}, []string{"description", strings.Repeat("more ", 80)}, } // Prep the test file testPath := filepath.Join(os.TempDir(), "_GOTEST_READTAGFILE_bagit.txt") tagFile, _ := bagins.NewTagFile(testPath) for _, exp := range exp_list { tagFile.Data.AddField(*bagins.NewTagField(exp[0], exp[1])) } tagFile.Create() defer os.Remove(testPath) // Open and test parsing the file. tf, errs := bagins.ReadTagFile(testPath) for _, err := range errs { t.Error(err) } if len(tf.Data.Fields()) != 3 { t.Error("Expected 3 but returned", len(tf.Data.Fields()), "fields!") } fields := tagFile.Data.Fields() for idx, exp := range exp_list { if fields[idx].Label() != exp[0] { t.Error("Tag field", idx, "label", fields[idx].Label(), "is not expected value of", exp[0]) } if fields[idx].Value() != exp[1] { t.Error("Tag field", idx, "value", fields[idx].Value(), "is not expected value of", exp[1]) } } }