Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def with_empty_list(expect):
sample = Sample('abc')
sample.datafile.text = "empty_items: []"
logbreak("Getting attribute")
expect(sample.empty_items) == []
logbreak("Setting attribute")
sample.empty_items.append(4.2)
sample.empty_items.append("abc")
logbreak("Getting attribute")
expect(sample.empty_items) == [4.2, "abc"]
write(
'tmp/sample.yml',
"""
# Heading
name: a
score: 1.0 # Line
nested:
# Nested heading
name: n
score: 2
""",
)
logbreak("Loading")
sample.datafile.load()
sample.score = 3
sample.nested.score = 4
logbreak("Saving")
sample.datafile.save()
expect(read('tmp/sample.yml')) == dedent(
"""
# Heading
def with_getattribute(expect):
sample = Sample()
write(
'tmp/sample.yml',
"""
item: b
""",
)
logbreak("Getting attribute")
expect(sample.item) == 'b'
'tmp/sample.yml',
"""
language: python
python:
- 3.7
- 3.8
""",
)
logbreak("Inferring object")
sample = auto('tmp/sample.yml')
logbreak("Updating attribute")
sample.python.append(4)
logbreak("Reading file")
expect(read('tmp/sample.yml')) == dedent(
"""
language: python
[a]
level = 10
[a.b]
level = 20
[a.b.c]
level = 30
[a.b.c.d]
level = 40
""",
)
logbreak("Reading attribute")
expect(sample.a.level) == 10
expect(sample.a.b.level) == 20
expect(sample.a.b.c.level) == 30
expect(sample.a.b.c.d.level) == 40
score: 1.0 # Line
nested:
# Nested heading
name: n
score: 2
""",
)
logbreak("Loading")
sample.datafile.load()
sample.score = 3
sample.nested.score = 4
logbreak("Saving")
sample.datafile.save()
expect(read('tmp/sample.yml')) == dedent(
"""
# Heading
[a]
level = 1
[a.b]
level = 2
[a.b.c]
level = 3
[a.b.c.d]
level = 4
"""
)
logbreak("Modifying attribute")
sample.a.b.c.d.level = 99
expect(read('tmp/sample.toml')) == dedent(
"""
level = 0
[a]
level = 1
[a.b]
level = 2
[a.b.c]
level = 3
[a.b.c.d]
def with_builtin(expect):
sample = Sample('abc')
sample.datafile.text = "count: 1"
logbreak("Getting attribute")
expect(sample.count) == 1
logbreak("Setting attribute")
sample.count = 4.2
logbreak("Getting attribute")
expect(sample.count) == 4
def with_empty_list(expect):
sample = Sample('abc')
sample.datafile.text = "empty_items: []"
logbreak("Getting attribute")
expect(sample.empty_items) == []
logbreak("Setting attribute")
sample.empty_items.append(4.2)
sample.empty_items.append("abc")
logbreak("Getting attribute")
expect(sample.empty_items) == [4.2, "abc"]
def test_auto_with_sample_file(expect):
write(
'tmp/sample.yml',
"""
homogeneous_list:
- 1
- 2
heterogeneous_list:
- 1
- 'abc'
empty_list: []
""",
)
logbreak("Inferring object")
sample = auto('tmp/sample.yml')
logbreak("Reading attributes")
expect(sample.homogeneous_list) == [1, 2]
expect(sample.heterogeneous_list) == [1, 'abc']
expect(sample.empty_list) == []
logbreak("Updating attribute")
sample.homogeneous_list.append(3.4)
sample.heterogeneous_list.append(5.6)
sample.empty_list.append(7.8)
logbreak("Reading file")
expect(read('tmp/sample.yml')) == dedent(
"""
homogeneous_list: