|
| 1 | +/* ipp-usb - HTTP reverse proxy, backed by IPP-over-USB connection to device |
| 2 | + * |
| 3 | + * Copyright (C) 2020 and up by Alexander Pevzner (pzz@apevzner.com) |
| 4 | + * See LICENSE for license terms and conditions |
| 5 | + * |
| 6 | + * IPP tests |
| 7 | + */ |
| 8 | + |
| 9 | +package main |
| 10 | + |
| 11 | +import ( |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/OpenPrinting/goipp" |
| 15 | +) |
| 16 | + |
| 17 | +// TestNewIppAttrs tests newIppAttrs function |
| 18 | +func TestNewIppAttrs(t *testing.T) { |
| 19 | + type testData struct { |
| 20 | + in goipp.Attributes // Input attributes |
| 21 | + out goipp.Attributes // Resulting attributes |
| 22 | + } |
| 23 | + |
| 24 | + tests := []testData{ |
| 25 | + { |
| 26 | + // Normal data |
| 27 | + in: goipp.Attributes{ |
| 28 | + goipp.MakeAttr("mopria-certified", |
| 29 | + goipp.TagText, |
| 30 | + goipp.String("1.3")), |
| 31 | + goipp.MakeAttr("printer-make-and-model", |
| 32 | + goipp.TagText, |
| 33 | + goipp.String("test printer")), |
| 34 | + }, |
| 35 | + |
| 36 | + out: goipp.Attributes{ |
| 37 | + goipp.MakeAttr("mopria-certified", |
| 38 | + goipp.TagText, |
| 39 | + goipp.String("1.3")), |
| 40 | + goipp.MakeAttr("printer-make-and-model", |
| 41 | + goipp.TagText, |
| 42 | + goipp.String("test printer")), |
| 43 | + }, |
| 44 | + }, |
| 45 | + |
| 46 | + { |
| 47 | + // Duplicated attribute. First occurrence wins |
| 48 | + in: goipp.Attributes{ |
| 49 | + goipp.MakeAttr("mopria-certified", |
| 50 | + goipp.TagText, |
| 51 | + goipp.String("1.3")), |
| 52 | + goipp.MakeAttr("printer-make-and-model", |
| 53 | + goipp.TagText, |
| 54 | + goipp.String("test printer")), |
| 55 | + goipp.MakeAttr("printer-make-and-model", |
| 56 | + goipp.TagText, |
| 57 | + goipp.String("duplicate")), |
| 58 | + }, |
| 59 | + |
| 60 | + out: goipp.Attributes{ |
| 61 | + goipp.MakeAttr("mopria-certified", |
| 62 | + goipp.TagText, |
| 63 | + goipp.String("1.3")), |
| 64 | + goipp.MakeAttr("printer-make-and-model", |
| 65 | + goipp.TagText, |
| 66 | + goipp.String("test printer")), |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, test := range tests { |
| 72 | + attrs := newIppAttrs(test.in) |
| 73 | + out := attrs.export() |
| 74 | + |
| 75 | + if !out.Similar(test.out) { |
| 76 | + f := goipp.NewFormatter() |
| 77 | + f.Printf("newIppAttrs test failed:") |
| 78 | + |
| 79 | + f.Printf("input:") |
| 80 | + f.SetIndent(4) |
| 81 | + f.FmtAttributes(test.in) |
| 82 | + f.SetIndent(0) |
| 83 | + |
| 84 | + f.Printf("expected output:") |
| 85 | + f.SetIndent(4) |
| 86 | + f.FmtAttributes(test.out) |
| 87 | + f.SetIndent(0) |
| 88 | + |
| 89 | + f.Printf("present output:") |
| 90 | + f.SetIndent(4) |
| 91 | + f.FmtAttributes(out) |
| 92 | + f.SetIndent(0) |
| 93 | + |
| 94 | + t.Errorf("%s", f.String()) |
| 95 | + |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments