Skip to content

Commit

Permalink
Documentation update and add unit test
Browse files Browse the repository at this point in the history
- added unit test for ReadJSONToArray()
- Edited documentation
  • Loading branch information
JakeWnuk committed May 12, 2024
1 parent 1c8ca10 commit ec2d8dd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
10 changes: 6 additions & 4 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ There are some additional notes when importing data:
- When reading from standard input, the tool can detect chaining `ptt` commands
when the `-v` flag is used. This can be used to pipe multiple commands together.
- When reading from files, the tool can detect when `ptt` JSON output is used as input and will parse the JSON data.
- The `-b` flag can be used to bypass map creation and use stdout as primary output. This can be useful for working with large amounts of data.
- If the `-b` flag is used, the final output will be empty and all
filtering and duplication removal will be disabled.
- The `-b` flag can be used to bypass map creation and use stdout as primary output. This can be useful for working with large datasets. If the `-b` flag is used, the final output will be empty and all filtering and duplication removal will be disabled.
- The `-d [0-2]` flag can be used to enable debug output. This will show the data
object after all transformations have been applied. There are two (2) levels
of debug output that can be used.
Expand All @@ -77,7 +75,7 @@ There are some additional notes when importing data:
template file should contain a list of transformations and operations to apply
to the input data. The template file should be in JSON format.
- See `docs/template.json` ([link](https://github.com/JakeWnuk/ptt/blob/main/docs/template.json)) for an example.
- See `docs/templates/` ([link](https://github.com/JakeWnuk/ptt/blob/main/docs/templates/)) for more examples.
- See `docs/templates/` ([link](https://github.com/JakeWnuk/ptt/blob/main/templates/)) for more examples.

The `-f`, `-k`, `-r`, `-tf`, `-tp`, and `-u` flags can be used multiple times and have
their collective values combined. The rest of the flags can only be used once.
Expand Down Expand Up @@ -175,6 +173,10 @@ keywords above:
- `ppt -l 8-12`: Keep only items within a range of lengths.
- `ptt -m 10`: Keep only items with a minimum frequency.

#### Debug Formats:
- `ptt -d 1`: Enable debug mode with verbosity level 1.
- `ptt -d 2`: Enable debug mode with verbosity level 2.

#### Output Formats:
- `ptt -v`: Show verbose output.
- `ptt -vv`: Show statistics output.
Expand Down
40 changes: 40 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// - ReadFilesToMap()
// - LoadStdinToMap()
// - CombineMaps()
// - ReadJSONToArray()
//
// ** Transformation Functions **
// - ReverseString()
Expand Down Expand Up @@ -158,6 +159,45 @@ func TestCombineMaps(t *testing.T) {
}
}

// Unit Test for ReadJSONToArray()
func TestReadJSONToArray(t *testing.T) {
// Define a test case struct
type TestCase struct {
Input string
Output []models.TemplateFileOperation
}

type TestCases []TestCase

// Create a mock file system with example files
mockFs := &models.MockFileSystem{
Files: map[string][]byte{
"file1": []byte(`[{"StartIndex":0,"EndIndex":4,"Verbose":true,"ReplacementMask":"uldbs","Bypass":false,"TransformationMode":"append"}]`),
"file2": []byte(`[{"StartIndex":0,"EndIndex":4,"Verbose":true,"ReplacementMask":"uldbs","Bypass":false,"TransformationMode":"append"},{"StartIndex":0,"EndIndex":4,"Verbose":true,"ReplacementMask":"uldbs","Bypass":false,"TransformationMode":"append"}]`),
"file3": []byte(`[{"StartIndex":0,"EndIndex":4,"Verbose":true,"ReplacementMask":"uldbs","Bypass":false,"TransformationMode":"append"},{"StartIndex":0,"EndIndex":4,"Verbose":true,"ReplacementMask":"uldbs","Bypass":false,"TransformationMode":"append"},{"StartIndex":0,"EndIndex":4,"Verbose":true,"ReplacementMask":"uldbs","Bypass":false,"TransformationMode":"append"}]`),
},
}

// Define test cases
testCases := TestCases{
{"file1", []models.TemplateFileOperation{{StartIndex: 0, EndIndex: 4, Verbose: true, ReplacementMask: "uldbs", Bypass: false, TransformationMode: "append"}}},
{"file2", []models.TemplateFileOperation{{StartIndex: 0, EndIndex: 4, Verbose: true, ReplacementMask: "uldbs", Bypass: false, TransformationMode: "append"}, {StartIndex: 0, EndIndex: 4, Verbose: true, ReplacementMask: "uldbs", Bypass: false, TransformationMode: "append"}}},
{"file3", []models.TemplateFileOperation{{StartIndex: 0, EndIndex: 4, Verbose: true, ReplacementMask: "uldbs", Bypass: false, TransformationMode: "append"}, {StartIndex: 0, EndIndex: 4, Verbose: true, ReplacementMask: "uldbs", Bypass: false, TransformationMode: "append"}, {StartIndex: 0, EndIndex: 4, Verbose: true, ReplacementMask: "uldbs", Bypass: false, TransformationMode: "append"}}},
}

// Run test cases
for _, testCase := range testCases {
input := []string{testCase.Input}
output := testCase.Output

given := ReadJSONToArray(mockFs, input)
if given[0].StartIndex != output[0].StartIndex || given[0].EndIndex != output[0].EndIndex || given[0].Verbose != output[0].Verbose || given[0].ReplacementMask != output[0].ReplacementMask || given[0].Bypass != output[0].Bypass || given[0].TransformationMode != output[0].TransformationMode {
t.Errorf("ReadJSONToArray(%v) = %v; want %v", input, given, output)
}
}

}

// Unit Test for ReverseString()
func TestReverseString(t *testing.T) {

Expand Down

0 comments on commit ec2d8dd

Please sign in to comment.