# Copyright (c) 2017 The Regents of the University of Michigan.
# All Rights Reserved. Licensed according to the terms of the Revised
# BSD License. See LICENSE.txt for details.
from hamcrest.core.base_matcher import BaseMatcher

class ContainsMarcFields (BaseMatcher):

    marc_fields = (
            "bib",
            "callno",
            "oclc",
            "author",
            "title",
            "description",
            "years",
    )

    def _matches (self, item):
        try:
            return all(x in item for x in self.marc_fields) \
                    and len(item) == len(self.marc_fields)

        except:
            return False

    def describe_to (self, description):
        marc_str = ", ".join(repr(x) for x in self.marc_fields)

        description.append_text("a dictionary containing MARC fields") \
                   .append_text(" (i.e. {})".format(marc_str))

def contains_marc_fields():
    return ContainsMarcFields()

@@ -229,33 +229,37 @@
 %0A%0Aclass 
-ContainsMarcField
+IsDictWithExpectedKey
 s (BaseM
@@ -276,141 +276,76 @@
     
-marc_fields = (%0A            %22bib%22,%0A            %22callno%22,%0A            %22oclc%22,%0A            %22author%22,%0A            %22title%22,%0A            %22
+def __init__ (self, description, *args):%0A        self.description = 
 desc
@@ -351,18 +351,16 @@
 cription
-%22,
 %0A       
@@ -364,26 +364,26 @@
     
-    %22years%22,%0A    )
+self.fields = args
 %0A%0A  
@@ -467,29 +467,24 @@
 r x in self.
-marc_
 fields) %5C%0A  
@@ -523,29 +523,24 @@
 == len(self.
-marc_
 fields)%0A%0A   
@@ -627,24 +627,26 @@
         
-marc_
+field_li
 st
-r
  = %22, %22.
@@ -676,21 +676,16 @@
 elf.
-marc_
 fields)%0A
 %0A   
@@ -680,25 +680,24 @@
 fields)%0A
-%0A
         
 descript
@@ -692,32 +692,15 @@
     
-description.append_
 text
-(
+ = 
 %22a d
@@ -724,144 +724,383 @@
 ing 
-MARC fields%22) %5C%0A                   .append_text(%22 (i.e. %7B%7D)%22.format(marc_str))%0A%0Adef contains_marc_fields():%0A    return ContainsMarcFields(
+%7B%7D and only %7B%7D (i.e %7B%7D)%22.format(%0A                self.description, self.description, field_list)%0A%0A        description.append_text(text)%0A%0Adef contains_marc_fields():%0A    return IsDictWithExpectedKeys(%0A            %22MARC fields%22,%0A            %22bib%22,%0A            %22callno%22,%0A            %22oclc%22,%0A            %22author%22,%0A            %22title%22,%0A            %22description%22,%0A            %22years%22
 )%0A

Write better hamcrest matcher