示例#1
0
def test_lines():
    result = select(CODE_BLOCK_EXAMPLE, lines="2,6")
    assert result == ("this is the first line\n"
                      "\n"
                      "⋯\n"
                      "\n"
                      "this is a trailing line\n")
    def test_inside_block_nested_matching_blocks(self):
        result = select(
            textwrap.dedent("""
                  /* Some code before {} */
                  first {
                    first {
                      first {
                        /* The most deeply nested. */
                      } 
                    }
                  }
                  /* Some code after {} */
                """),
            inside_block="first",
        )
        self.maxDiff = None
        self.assertEquals(
            """  first {
    first {
      /* The most deeply nested. */
    } 
  }
""",
            result,
        )
    def test_inside_block_contains_keyword(self):
        result = select(
            textwrap.dedent("""
                  /* Some code before {} */
                  first {
                    /* first */
                    first();
                    if (first()) {
                      first();
                    } else {
                      first();
                    }
                  }
                  /* Some code after {} */
                """),
            inside_block="first",
        )
        self.maxDiff = None
        self.assertEquals(
            """  /* first */
  first();
  if (first()) {
    first();
  } else {
    first();
  }
""",
            result,
        )
 def test_inside_block_curly_on_same_line(self):
     result = select(
         textwrap.dedent("""
             foo {
               /* {} */
             }
             """),
         inside_block="foo",
     )
     self.assertEquals("  /* {} */\n", result)
 def test_lines(self):
     result = select(CODE_BLOCK_EXAMPLE, lines="2,6")
     self.assertEquals(
         ("this is the first line\n"
          "\n"
          "⋯\n"
          "\n"
          "this is a trailing line\n"),
         result,
     )
 def test_inside_block_multiple_curly_on_same_line(self):
     result = select(
         textwrap.dedent("""
             //
             foo {
               /* {} {@code bar} {@link baz} */
             }
             """),
         inside_block="foo",
     )
     self.assertEquals("  /* {} {@code bar} {@link baz} */\n", result)
 def test_inside_block_in_a_block(self):
     result = select(
         textwrap.dedent("""
             {{{
             foo {
               /* inside foo */
             }
             }}}
             """),
         inside_block="foo",
     )
     self.assertEquals("  /* inside foo */\n", result)
 def test_inside_block_content_on_last_line(self):
     result = select(
         textwrap.dedent("""
             foo {
               if (true) {
                 bar();
               } } 
             /* The line above contains both the closing curly bracket for `if` and for `foo` */
             """),
         inside_block="foo",
     )
     self.assertEquals(("  if (true) {\n" "    bar();\n"), result)
 def test_block_curly_on_same_line(self):
     result = select(
         textwrap.dedent("""
             /* Before foo */
             foo {
               /* {} {@code Bar} */
             }
             /* After foo */
             """),
         block="foo",
     )
     self.assertEquals(("foo {\n" "  /* {} {@code Bar} */\n" "}\n"), result)
def get_substitute(page, title, filename, lines, block, inside_block):

    page_parent_dir = os.path.dirname(page.file.abs_src_path)
    import_path = os.path.join(page_parent_dir, filename)
    with open(import_path) as f:
        content = f.read()

    selected_content = select(
        content, lines=lines, block=block, inside_block=inside_block
    )

    dedented = textwrap.dedent(selected_content)

    return '\n```java tab="' + title + '"\n' + dedented + "\n```\n\n"
 def test_inside_block_multiple_blocks_second(self):
     result = select(
         textwrap.dedent("""
               /* Some code before {} */
               first {
                 /* inside first */
               }
               /* Some code in between */
               second {
                 /* inside second */
               }
               /* Some code after {} */
             """),
         inside_block="second",
     )
     self.maxDiff = None
     self.assertEquals("  /* inside second */\n", result)
 def test_inside_block_several_matching_blocks(self):
     result = select(
         textwrap.dedent("""
               /* Some code before {} */
               matching_block 1 {
                 /* inside first */
               }
               /* Some code in between */
               matching_block 2 {
                 /* inside second */
               }
               /* Some code after {} */
             """),
         inside_block="matching_block",
     )
     self.maxDiff = None
     self.assertEquals(("  /* inside first */\n"
                        "\n"
                        "  ⋯\n\n"
                        "  /* inside second */\n"), result)
示例#13
0
def test_whole_block():
    result = select(CODE_BLOCK_EXAMPLE, block="blockstarter")
    assert result == ("blockstarter {\n" "    block content\n" "}\n")
示例#14
0
def test_inside_block():
    result = select(CODE_BLOCK_EXAMPLE, inside_block="blockstarter")
    assert result == "    block content\n"
 def test_whole_block(self):
     result = select(CODE_BLOCK_EXAMPLE, block="blockstarter")
     self.assertEquals(("blockstarter {\n"
                        "    block content\n"
                        "}\n"), result)
 def test_inside_block(self):
     result = select(CODE_BLOCK_EXAMPLE, inside_block="blockstarter")
     self.assertEquals("    block content\n", result)