Coder Social home page Coder Social logo

objc-palindrome-inspector-al's Introduction

Palindrome Inspector

Objectives

  1. Use a familiar problem to introduce yourself to Xcode's testing interface.
  2. Compare the "expected" versus the "got" of each test failure to gain insight towards how to solve the problem.
  3. Solve the problem step by step, relying on the tests to ensure yourself of forward progress.
  4. Verify that Learn detected your successful solution by looking at the "Local Build" light.

Introduction

Instead of relying on NSLog() statements to inspect our palindrome detector, let's rely on a testing suite that uses Specta and Expecta! The test file is already set up for you with the test strings and the method calls. You'll have to write out the method implementations a second time, but now you'll have the test failures and successes to guide you instead of doing it yourself from the application:didFinishLaunchingWithOptions: methods.

Instructions

Fork and clone this lab.

Open the objc-palindrome-inspector.xcworkspace file. Note: This is the workspace, not the project. The workspace contains a second project that holds the testing library that we use.

1 — Try to run the tests with U. You should experience a build failure that generates a collection of errors because of missing method definitions.

Don't panic! This is because the test file FISAppDelegateSpec.m is looking for the methods that you're going to write. Objective-C doesn't have a NoMethodError like some other languages such as Ruby, so instead the pre-compiler simply keeps the build from succeeding in order to avoid a definite crash. The fix for this is simple: write the methods the test file is instructed to call!

2 — Navigate to the FISAppDelegate.h header file and declare the same two methods from the previous lab:

  • stringIsPalindrome: which takes one NSString argument called string and returns a BOOL, and
  • stringByReversingString: which takes one NSString argument called string and returns an NSString.

3 — Now, navigate to the FISAppDelegate.m implementation file and use autocomplete to define the implementation of stringIsPalindrome: to return NO; and the implementation of stringByReversingString: to return nil;.

4 — You should now be able to run the tests with U. You should see that most of the tests fail (the ones that succeed are negative checks). These are test failures, not errors, and are marked with a red diamond containing an "x". Successes are marked with a green diamond containing a check mark.

You can (temporarily) drag the Navigator area's pane out to the right in order to view the descriptions of each test, which can sometimes be rather long. Unfortunately there is not a way to get Xcode to wrap this text to a new line in the Navigator area so dragging the pane to be wider is common practice.

Alternatively, you can view the test results in the Results navigator by clicking on the right-most symbol in the top of the Navigator area:

Note: The Results navigator is useful for comparing long test results that do not fit into the highlighted flags that appear in the Code Editor when viewing a test file.

5 — Insert the provided implementation for the stringByReversingString: method which returns a string that is the reverse of the argument string:

- (NSString *)stringByReversingString:(NSString *)string {
    
    NSString *result = @"";
    
    for (NSUInteger i = [string length]; i > 0; i--) {
        NSUInteger index = i - 1;
        unichar c = [string characterAtIndex:index];
        result = [result stringByAppendingFormat:@"%c", c];
    }
    
    return result;
}

Run the tests again with U. You should see the tests for this method succeed and their lights turn to green check marks:

6 — Now, solve the stringIsPalindrome: method step-by-step, relying on the testing suite to check each portion of the problem. Avoid copy/pasting your code from the code-along; try to rely on the tests to help you solve a familiar problem. Only reference the code-along if you get stuck. Remember, the initial steps to detect a single-word, all-lowercase palindrome is to:

  • reverse the string,
  • compare the original string to its reverse, and
  • return the result of the comparison.

Correctly implementing these steps should allow the stringIsPalindrome__returns_YES_for_the_palindrome_racecar test to pass without causing any of the four passing tests to fail.

7 — Add functionality to the stringIsPalindrome: method to handle uppercase strings. This should allow the tests using the palindromes "Bob", "Kanakanak", and "Aibohphobia" to pass without causing any of the other five passing tests to fail.

8 — Add functionality to the stringIsPalindrome: method to handle a palindrome phrase by removing the spaces from the string being checked. This should allow the tests using the palindrome phrases "never odd or even" and "I prefer pi" to pass without causing any of the other eight passing tests to fail.

9 — Add functionality to the stringIsPalindrome: method to handle a palindrome sentence by removing punctuation from the string being checked. Hint: This step requires a loop. It should allow the tests using the palindrome sentences "Flee to me, remote elf."; "Norma is as selfless as I am, Ron."; and "No sir! Away! A papaya war is on!" to pass without causing any of the other ten tests to fail.

10 — At this point all thirteen of the tests should be passing:

Congratulations! You have solved your first tested lab. Your "local build" light in Learn should now be green:

Commit and push your solution to your remote fork. On GitHub, open a pull request to complete the lab.

View Palindrome Inspector on Learn.co and start learning to code for free.

View Palindrome Inspector on Learn.co and start learning to code for free.

objc-palindrome-inspector-al's People

Contributors

markedwardmurray avatar annjohn avatar dennisvera avatar ipc103 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.