github.com/boki/go-xmp@v1.0.1/test/model_test.go (about)

     1  // Copyright (c) 2017-2018 Alexander Eichhorn
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // 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, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	_ "trimmer.io/go-xmp/models"
    22  	"trimmer.io/go-xmp/xmp"
    23  )
    24  
    25  type TestModel struct {
    26  	S1    string                  `test:"s1"      xmp:"test:s1"`
    27  	T1    TestType1               `test:"t1"      xmp:"test:t1"`      // Text (Un)Marshaler
    28  	T2    TestType2               `test:"t2"      xmp:"test:t2"`      // XMP (Un)Marshaler
    29  	T3    TestType3               `test:"t3"      xmp:"test:t3"`      // Binary Unmarshaler
    30  	A1    TestType4               `test:"a1"      xmp:"test:a1,attr"` // XMP Attr Marshaler
    31  	None  string                  `test:"-"       xmp:"-"`            // unexported
    32  	Empty string                  `test:"e"       xmp:"test:e,empty"` // export even if empty
    33  	Ext   xmp.NamedExtensionArray `test:"ext,any" xmp:"test:ext,any"` // any flag for ext
    34  }
    35  
    36  var NsTest = xmp.NewNamespace("test", "http://ns.example.com/test/1.0/", NewTestModel)
    37  
    38  func init() {
    39  	xmp.Register(NsTest)
    40  }
    41  
    42  func NewTestModel(name string) xmp.Model {
    43  	return &TestModel{}
    44  }
    45  
    46  func (m *TestModel) Namespaces() xmp.NamespaceList {
    47  	return xmp.NamespaceList{NsTest}
    48  }
    49  
    50  func (m *TestModel) Can(nsName string) bool {
    51  	return nsName == NsTest.GetName()
    52  }
    53  
    54  func (x *TestModel) SyncModel(d *xmp.Document) error {
    55  	return nil
    56  }
    57  
    58  func (x *TestModel) SyncFromXMP(d *xmp.Document) error {
    59  	return nil
    60  }
    61  
    62  func (x TestModel) SyncToXMP(d *xmp.Document) error {
    63  	return nil
    64  }
    65  
    66  func (x *TestModel) CanTag(tag string) bool {
    67  	_, err := xmp.GetNativeField(x, tag)
    68  	return err == nil
    69  }
    70  
    71  func (x *TestModel) GetTag(tag string) (string, error) {
    72  	if v, err := xmp.GetNativeField(x, tag); err != nil {
    73  		return "", fmt.Errorf("%s: %v", NsTest.GetName(), err)
    74  	} else {
    75  		return v, nil
    76  	}
    77  }
    78  
    79  func (x *TestModel) SetTag(tag, value string) error {
    80  	if err := xmp.SetNativeField(x, tag, value); err != nil {
    81  		return fmt.Errorf("%s: %v", NsTest.GetName(), err)
    82  	}
    83  	return nil
    84  }
    85  
    86  type TestType1 string
    87  type TestType2 string
    88  type TestType3 string
    89  type TestType4 string
    90  
    91  func (t *TestType1) UnmarshalText(b []byte) error {
    92  	s := string(b)
    93  	switch s {
    94  	case "error":
    95  		return fmt.Errorf("unmarshal text error")
    96  	case "x":
    97  		*t = "y"
    98  	default:
    99  		*t = TestType1(s)
   100  	}
   101  	return nil
   102  }
   103  
   104  func (t *TestType2) UnmarshalXMP(d *xmp.Decoder, node *xmp.Node, m xmp.Model) error {
   105  	return nil
   106  }
   107  
   108  func (t *TestType3) UnmarshalBinary(b []byte) error {
   109  	return nil
   110  }
   111  
   112  func (t *TestType4) UnmarshalAttr(d *xmp.Decoder, node *xmp.Node, m xmp.Model) error {
   113  	return nil
   114  }
   115  
   116  // use `any` field of a model that supports it
   117  func TestPathSetAny(T *testing.T) {
   118  }
   119  
   120  func TestPathGetAny(T *testing.T) {
   121  }
   122  
   123  func TestPathSetNamedExtension(T *testing.T) {
   124  }
   125  
   126  func TestPathGetNamedExtension(T *testing.T) {
   127  }
   128  
   129  func TestStructTagMinus(T *testing.T) {}
   130  func TestStructTagEmpty(T *testing.T) {}
   131  func TestStructTagAttr(T *testing.T)  {}
   132  
   133  func TestSetNativeTag(T *testing.T) {}
   134  func TestGetNativeTag(T *testing.T) {}
   135  
   136  func TestTextMarshaler(T *testing.T)      {}
   137  func TestTextUnmarshaler(T *testing.T)    {}
   138  func TestXmpAttrMarshaler(T *testing.T)   {}
   139  func TestXmpAttrUnmarshaler(T *testing.T) {}
   140  func TestXmpMarshaler(T *testing.T)       {}
   141  func TestXmpUnmarshaler(T *testing.T)     {}
   142  func TestBinaryUnmarshaler(T *testing.T) {
   143  	// m.SetTag()
   144  }