Back to Repositories

Validating YAML Line Parsing Implementation in Apktool

This test suite validates YAML parsing functionality in the Apktool library, focusing on line-by-line YAML content analysis. It ensures proper handling of YAML syntax elements including empty lines, comments, key-value pairs, and list items.

Test Coverage Overview

The test suite provides comprehensive coverage of YAML line parsing functionality.

  • Empty line detection and indentation handling
  • Comment parsing and extraction
  • Key-value pair parsing with various formats
  • List item identification and processing
  • Edge cases including special characters and whitespace handling

Implementation Analysis

The testing approach uses JUnit framework with systematic test case organization. Each test method focuses on a specific YAML line type, utilizing the YamlLine class to parse and validate different components.

The implementation leverages JUnit’s assertion methods for verification, with careful attention to indentation levels, key-value extraction, and special character handling.

Technical Details

  • Testing Framework: JUnit
  • Base Class: BaseTest
  • Test Subject: YamlLine class
  • Key Methods: isEmpty, isComment, getKey(), getValue()
  • Assertion Types: assertEquals, assertTrue, assertFalse

Best Practices Demonstrated

The test suite demonstrates several testing best practices.

  • Atomic test methods focusing on single functionality
  • Clear test method naming conventions
  • Comprehensive edge case coverage
  • Consistent assertion patterns
  • Well-organized test structure extending from BaseTest

ibotpeaches/apktool

brut.apktool/apktool-lib/src/test/java/brut/androlib/apk/YamlLineTest.java

            
/*
 *  Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
 *  Copyright (C) 2010 Connor Tumbleson <[email protected]>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package brut.androlib.apk;

import brut.androlib.BaseTest;

import org.junit.*;
import static org.junit.Assert.*;

public class YamlLineTest extends BaseTest {

    @Test
    public void testEmptyLine() {
        YamlLine line = new YamlLine("");
        assertEquals(0, line.indent);
        assertTrue(line.isEmpty);

        line = new YamlLine(" ");
        assertEquals(0, line.indent);
        assertTrue(line.isEmpty);
    }

    @Test
    public void testComment() {
        YamlLine line = new YamlLine("!ApkInfo.class");
        assertTrue(line.isComment);

        line = new YamlLine("# This is comment");
        assertEquals(0, line.indent);
        assertTrue(line.isComment);
        assertEquals("", line.getKey());
        assertEquals("This is comment", line.getValue());

        line = new YamlLine("  # This is comment");
        assertEquals(2, line.indent);
        assertTrue(line.isComment);
        assertEquals("", line.getKey());
        assertEquals("This is comment", line.getValue());
    }

    @Test
    public void testKeyLine() {
        YamlLine line = new YamlLine("name:");
        assertFalse(line.isComment);
        assertEquals(0, line.indent);
        assertEquals("name", line.getKey());
        assertEquals("", line.getValue());

        line = new YamlLine("  name:");
        assertFalse(line.isComment);
        assertEquals(2, line.indent);
        assertEquals("name", line.getKey());
        assertEquals("", line.getValue());

        line = new YamlLine(":value");
        assertFalse(line.isComment);
        assertEquals(0, line.indent);
        assertEquals("", line.getKey());
        assertEquals("value", line.getValue());

        line = new YamlLine("  : value ");
        assertFalse(line.isComment);
        assertEquals(2, line.indent);
        assertEquals("", line.getKey());
        assertEquals("value", line.getValue());

        line = new YamlLine("name  : value ");
        assertFalse(line.isComment);
        assertEquals(0, line.indent);
        assertEquals("name", line.getKey());
        assertEquals("value", line.getValue());

        line = new YamlLine("  name  : value ");
        assertFalse(line.isComment);
        assertEquals(2, line.indent);
        assertEquals("name", line.getKey());
        assertEquals("value", line.getValue());

        line = new YamlLine("  name  : value ::");
        assertFalse(line.isComment);
        assertEquals(2, line.indent);
        assertEquals("name", line.getKey());
        assertEquals("value", line.getValue());

        // split this gives parts.length = 0!!
        line = new YamlLine(":::");
        assertFalse(line.isComment);
        assertEquals(0, line.indent);
        assertEquals("", line.getKey());
        assertEquals("", line.getValue());
    }

    @Test
    public void testItemLine() {
        YamlLine line = new YamlLine("- val1");
        assertTrue(line.isItem);
        assertEquals(0, line.indent);
        assertEquals("", line.getKey());
        assertEquals("val1", line.getValue());

        line = new YamlLine("  - val1: ff");
        assertTrue(line.isItem);
        assertEquals(2, line.indent);
        assertEquals("", line.getKey());
        assertEquals("val1: ff", line.getValue());
    }
}