예제 #1
0
 def test_string_representation(self):
     one_quart = Size(quantity=1, unit="quart", price="$9")
     vanilla = Flavor("vanilla")
     quart_of_vanilla = IceCream(flavor=vanilla, size=one_quart)
     self.assertEqual(str(quart_of_vanilla), '1 quart of vanilla')
     self.assertEqual(str(quart_of_vanilla), '1 quart of vanilla')
     two_scoops = IceCream(
         flavor=Flavor("chocolate"),
         size=Size(quantity=2, unit="scoop", price="$3"),
     )
     self.assertEqual(str(two_scoops), '2 scoops of chocolate')
예제 #2
0
def atomconvert(text, nts):
    parr = []  # eg.[a 1 b 2 ]
    atomparr = []  # eg.[1 2 3]
    parr.clear()
    atomparr.clear()

    sumflag = 0
    for chip in reversed(text):
        # print(chip)
        if "]" in chip:
            sumflag = sumflag + chip.count("]")
            # print(sumflag)
        chip = chip.replace("[", "")
        chip = chip.replace("]", "")
        # print(chip)

        if is_number(chip):  # 如果是数字

            atomparr.append(float(chip))

        else:  # 如果是字母
            list.reverse(atomparr)

            if chip == "a" or chip == "alpha":
                parr.append(Alpha(*atomparr))

            elif chip == "b" or chip == "brightness":
                parr.append(Brightness(*atomparr))

            elif chip == "sat" or chip == "saturation":
                parr.append(Saturation(*atomparr))

            elif chip == "h" or chip == "hue":
                parr.append(Hue(*atomparr))

            elif chip == "y":
                parr.append(Y(*atomparr))

            elif chip == "r" or chip == "rotate":
                parr.append(Rotate(*atomparr))

            elif chip == "f" or chip == "flip":
                parr.append(Flip(*atomparr))

            elif chip == "x":
                parr.append(X(*atomparr))

            elif chip == "s" or chip == "size":
                parr.append(Size(*atomparr))

            elif chip == "trans" or chip == "transform":
                parr.append(Transform(*atomparr))

            elif chip == "skew":
                parr.append(Skew(*atomparr))

            elif chip == "..":
                parr.append(randRange(*atomparr))

            # 统计完毕一个参数,清空数字列表
            atomparr.clear()

    list.reverse(parr)

    if sumflag == 2:
        # print("GG")
        parr = [Leftbrac()] + parr + [Rightbrac()]

    if text[0] == "CIRCLE":
        return Circle(parr)

    elif text[0] == "TRIANGLE":
        return Triangle(parr)

    elif text[0] == "SQUARE":
        return Square(parr)

    else:
        ntName = text[0]
        for n in nts:
            if ntName == n.name:
                return RuleCall(n, parr)

        newNT = NonTerminal(ntName, [])
        nts.append(newNT)
        return RuleCall(newNT, parr)
예제 #3
0
 def test_initializer(self):
     one_quart = Size(quantity=1, unit="quart", price="$9")
     vanilla = Flavor("vanilla")
     quart_of_vanilla = IceCream(flavor=vanilla, size=one_quart)
     self.assertEqual(quart_of_vanilla.size, one_quart)
     self.assertEqual(quart_of_vanilla.flavor, vanilla)
예제 #4
0
 def test_machine_string_representation(self):
     size = Size(quantity=1, unit="cup", price="5")
     self.assertEqual(repr(size), "Size(quantity=1, unit='cup', price='5')")
예제 #5
0
 def test_pluralization(self):
     size = Size(quantity=3, unit="pint", price="9.00")
     self.assertEqual(str(size), "3 pints")
     size = Size(quantity=3, unit="scoop", price="4.00")
     self.assertEqual(str(size), "3 scoops")
예제 #6
0
 def test_human_string_representation(self):
     size = Size(quantity=1, unit="gram", price="5.00")
     self.assertEqual(str(size), "1 gram")
     size = Size(quantity=1, unit="scoop", price="5.00")
     self.assertEqual(str(size), "1 scoop")
예제 #7
0
 def test_initializer(self):
     size = Size(quantity=1, unit="gram", price="5.00")
     self.assertEqual(size.quantity, 1)
     self.assertEqual(size.unit, "gram")
     self.assertEqual(size.price, "5.00")