1// Copyright 2016 Richard Lehane. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package writer
16
17import (
18	"bufio"
19	"io/ioutil"
20	"os"
21	"testing"
22	"time"
23
24	"github.com/richardlehane/siegfried/pkg/config"
25	"github.com/richardlehane/siegfried/pkg/core"
26)
27
28var testValues = []string{"pronom",
29	"fmt/43",
30	"JPEG File Interchange Format",
31	"1.01",
32	"image/jpeg",
33	"extension match jpg; byte match at [[[0 14]] [[75201 2]]]",
34	""}
35
36type testErr struct{}
37
38func (t testErr) Error() string { return "mscfb: bad OLE" }
39
40type testID struct{}
41
42func (t testID) String() string          { return testValues[1] }
43func (t testID) Known() bool             { return true }
44func (t testID) Warn() string            { return "" }
45func (t testID) Values() []string        { return testValues }
46func (t testID) Archive() config.Archive { return 0 }
47
48func makeFields() []string {
49	return []string{"namespace",
50		"id",
51		"format",
52		"version",
53		"mime",
54		"basis",
55		"warning"}
56}
57
58func TestYAMLHeader(t *testing.T) {
59	expect := "  - ns      : %v\n    id      : %v\n    format  : %v\n    version : %v\n    mime    : %v\n    basis   : %v\n    warning : %v\n"
60	ret := header(makeFields())
61	if expect != ret {
62		t.Errorf("Expecting header to return %s\nGot: %s", expect, ret)
63	}
64}
65
66func ExampleYAML() {
67	yml := YAML(ioutil.Discard)
68	yml.Head("", time.Time{}, time.Time{}, [3]int{}, [][2]string{{"pronom", ""}}, [][]string{makeFields()}, "")
69	yml.(*yamlWriter).w = bufio.NewWriter(os.Stdout)
70	yml.File("example.doc", 1, "2015-05-24T16:59:13+10:00", nil, testErr{}, []core.Identification{testID{}})
71	yml.Tail()
72	// Output:
73	// ---
74	// filename : 'example.doc'
75	// filesize : 1
76	// modified : 2015-05-24T16:59:13+10:00
77	// errors   : 'mscfb: bad OLE'
78	// matches  :
79	//   - ns      : 'pronom'
80	//     id      : 'fmt/43'
81	//     format  : 'JPEG File Interchange Format'
82	//     version : '1.01'
83	//     mime    : 'image/jpeg'
84	//     basis   : 'extension match jpg; byte match at [[[0 14]] [[75201 2]]]'
85	//     warning :
86}
87
88func ExampleJSON() {
89	js := JSON(ioutil.Discard)
90	js.Head("", time.Time{}, time.Time{}, [3]int{}, [][2]string{{"pronom", ""}}, [][]string{makeFields()}, "")
91	js.(*jsonWriter).w = bufio.NewWriter(os.Stdout)
92	js.File("example.doc", 1, "2015-05-24T16:59:13+10:00", nil, testErr{}, []core.Identification{testID{}})
93	js.Tail()
94	// Output:
95	// {"filename":"example.doc","filesize": 1,"modified":"2015-05-24T16:59:13+10:00","errors": "mscfb: bad OLE","matches": [{"ns":"pronom","id":"fmt/43","format":"JPEG File Interchange Format","version":"1.01","mime":"image/jpeg","basis":"extension match jpg; byte match at [[[0 14]] [[75201 2]]]","warning":""}]}]}
96}
97