def test_add_open_tag_semantic_penalty():
    tag = LinearizedTag.default()
    obs = Observation(None, None, None, None, tag)

    # test add class
    btnOpen = AddOpenTagAction(0, "button")
    divOpen = AddOpenTagAction(0, "div")

    action_exec_should_success(obs, btnOpen)
    assert compute_semantic_penalty(obs, divOpen, -50) == -50
def test_add_open_tag():
    tag = LinearizedTag.default()

    btnOpen = AddOpenTagAction(0, "button")
    btnClose = AddCloseTagAction(1)

    obs = Observation(None, None, None, None, tag)

    action_exec_should_success(obs, btnOpen)
    action_exec_should_success(obs, btnClose)
    action_exec_should_fail(obs, btnClose)
def test_linearized_tag():
    tag = LinearizedTag.default()
    tag.add_open_tag("div")

    assert tag.to_body() == "<div></div>"

    tag.add_open_tag("p")
    tag.add_class("p", "header")
    tag.add_class("p", "bold")

    tag.add_text("vi sao")

    assert tag.to_body() == '<div><p class="header bold">vi sao</p></div>'
def test_add_class_semantic_penalty():
    tag = LinearizedTag.default()
    obs = Observation(None, None, None, None, tag)

    # test add class
    btnOpen = AddOpenTagAction(0, "button")
    btnSuccess = AddClassAction(3, "button", "btn-success")
    btnDanger = AddClassAction(3, "button", "btn-danger")

    action_exec_should_success(obs, btnOpen)
    assert compute_semantic_penalty(obs, btnSuccess, -50) == 0
    action_exec_should_success(obs, btnSuccess)
    assert compute_semantic_penalty(obs, btnDanger, -50) == -50
    def to_linearized_tag(self):
        ltag = LinearizedTag.default()
        for tag in self.tags:
            if tag[0] == 'program':
                continue

            if tag[1] == self.OPEN_TAG:
                ltag.add_tag_and_class(tag[0], tag[2])
            elif tag[1] == self.CLOSE_TAG:
                ltag.add_close_tag()
            else:
                assert tag[1] == self.SPECIAL_TOKEN
                ltag.add_text(tag[0])
        return ltag
def test_semantic_penalty():
    obs = Observation(None, None, None, None, None)
    obs.curr_tag = LinearizedTag.default()

    container = AddOpenTagAndClassAction(0, "div", ("container-fluid", ))
    row = AddOpenTagAndClassAction(0, "div", ("row", ))
    col = AddOpenTagAndClassAction(0, "div", ("col-4", ))
    box = AddOpenTagAndClassAction(0, "div", ("grey-background", ))

    assert compute_semantic_penalty(obs, container, -50) == 0
    action_exec_should_success(obs, container)
    assert compute_semantic_penalty(obs, row, -50) == 0
    action_exec_should_success(obs, row)
    assert compute_semantic_penalty(obs, col, -50) == 0
    action_exec_should_success(obs, col)
    assert compute_semantic_penalty(obs, container, -50) == -50
    assert compute_semantic_penalty(obs, box, -50) == 0
    action_exec_should_success(obs, box)
 def reset(self):
     self.obs = Observation(self.env_idx, np.zeros_like(self.get_target_state()), LinearizedTag.default())
    def __init__(self, env_creator: EnvCreator, env_idx: int):
        self.env_creator = env_creator
        self.env_idx = env_idx

        self.obs = Observation(env_idx, np.zeros_like(env_creator.sketches[env_idx]), LinearizedTag.default())
        self.teacher: Teacher = None