Exemplo n.º 1
0
 def test_flag_name_error(self):
   with self.assertRaisesRegex(ValueError, "must be a string"):
     ff.DEFINE_dict(
         ff.String("name", "string flag"),
         ff.String("stringflag", "string"),
         integer_field=ff.Integer(1, "integer field")
     )
Exemplo n.º 2
0
 def test_too_many_positional_args_error(self):
   with self.assertRaisesRegex(ValueError, "at most two positional"):
     ff.DEFINE_dict(
         "name",
         ff.String("foo", "string"),
         ff.String("bar", "string"),
         integer_field=ff.Integer(1, "integer field")
     )
Exemplo n.º 3
0
  def test_define_flat(self):
    flagholder = ff.DEFINE_dict(
        "flat_dict",
        integer_field=ff.Integer(1, "integer field"),
        string_field=ff.String(""),
        string_list_field=ff.StringList(["a", "b", "c"], "string list field"))

    # This should return a single dict with the default values specified above.
    expected = {
        "integer_field": 1,
        "string_field": "",
        "string_list_field": ["a", "b", "c"]
    }
    self.assertEqual(FLAGS.flat_dict, expected)
    self.assertEqual(flagholder.value, expected)

    # These flags should also exist, although we won't access them in practice.
    self.assertEqual(FLAGS["flat_dict.integer_field"].value, 1)
    self.assertEqual(FLAGS["flat_dict.string_field"].value, "")

    # Custom help string.
    self.assertEqual(FLAGS["flat_dict.integer_field"].help, "integer field")
    # Default help string.
    self.assertEqual(FLAGS["flat_dict.string_field"].help,
                     "flat_dict.string_field")
Exemplo n.º 4
0
 def test_flag_values_error(self):
   with self.assertRaisesRegex(ValueError, "FlagValues instance"):
     ff.DEFINE_dict(
         "name",
         ff.String("stringflag", "string"),
         integer_field=ff.Integer(1, "integer field")
     )
Exemplo n.º 5
0
 def test_invalid_flat_leaf(self):
     expected_message = ff._NOT_A_DICT_OR_ITEM.format("int")
     with self.assertRaisesWithLiteralMatch(TypeError, expected_message):
         ff._extract_defaults({
             "string_field":
             ff.String("default", "String field"),
             "naughty_field":
             100,
         })
Exemplo n.º 6
0
 def test_valid_flat(self):
     result = ff._extract_defaults({
         "integer_field":
         ff.Integer(10, "Integer field"),
         "string_field":
         ff.String("default", "String field"),
     })
     expected = {"integer_field": 10, "string_field": "default"}
     self.assertEqual(result, expected)
Exemplo n.º 7
0
 def test_invalid_container(self):
     expected_message = ff._NOT_A_DICT_OR_ITEM.format("list")
     with self.assertRaisesWithLiteralMatch(TypeError, expected_message):
         ff._extract_defaults({
             "integer_field":
             ff.Integer(10, "Integer field"),
             "string_field":
             ff.String("default", "String field"),
             "nested": [ff.Float(3.1, "Float field")],
         })
Exemplo n.º 8
0
    def test_define_nested(self):
        flagholder = ff.DEFINE_dict(
            "nested_dict",
            integer_field=ff.Integer(1, "integer field"),
            sub_dict=dict(string_field=ff.String("", "string field")))

        # This should return a single dict with the default values specified above.
        expected = {"integer_field": 1, "sub_dict": {"string_field": ""}}
        self.assertEqual(FLAGS.nested_dict, expected)
        self.assertEqual(flagholder.value, expected)

        # These flags should also exist, although we won't access them in practice.
        self.assertEqual(FLAGS["nested_dict.integer_field"].value, 1)
        self.assertEqual(FLAGS["nested_dict.sub_dict.string_field"].value, "")
Exemplo n.º 9
0
 def test_valid_nested(self):
   result = ff._extract_defaults(
       {
           "integer_field": ff.Integer(10, "Integer field"),
           "string_field": ff.String("default", "String field"),
           "nested": {
               "float_field": ff.Float(3.1, "Float field"),
           },
       }
   )
   expected = {
       "integer_field": 10,
       "string_field": "default",
       "nested": {"float_field": 3.1},
   }
   self.assertEqual(result, expected)