require "test_helper"
class TestXcodeMacros < Test::Unit::TestCase
  def test_process
    keys = ["login.title", "login.button"]
    table = "Localizable"
    xcode_macros = Babelish::XcodeMacros.new
    content = xcode_macros.process(table, keys)
    expected_output = String.new(<<-EOS)
#define LS_LOGIN_TITLE NSLocalizedStringFromTable(@"login.title",@"Localizable",@"")
#define LS_LOGIN_BUTTON NSLocalizedStringFromTable(@"login.button",@"Localizable",@"")
    EOS
    assert_equal expected_output, content
  end

  def test_write_macros_class_method
    keys = ["login.title", "login.button"]
    table = "Localizable"
    Babelish::XcodeMacros.write_macros("Babelish.h", table, keys)
    expected_output = String.new(<<-EOS)
//
//  Babelish.h
//  
//  This file was generated by Babelish
//  
//  https://github.com/netbe/babelish
//
#define LS_LOGIN_TITLE NSLocalizedStringFromTable(@"login.title",@"Localizable",@"")
#define LS_LOGIN_BUTTON NSLocalizedStringFromTable(@"login.button",@"Localizable",@"")
    EOS
    assert File.exist?("Babelish.h")
    result = File.read("Babelish.h")
    assert_equal expected_output, result
    # clean up
    system("rm -f ./Babelish.h")
  end

  def test_write_macros_process
    keys = ["login.title", "login.button"]
    table = "Localizable"
    macros = Babelish::XcodeMacros.new
    macros.process(table, keys)
    macros.write_content("Babelish.h")
    expected_output = String.new(<<-EOS)
//
//  Babelish.h
//  
//  This file was generated by Babelish
//  
//  https://github.com/netbe/babelish
//
#define LS_LOGIN_TITLE NSLocalizedStringFromTable(@"login.title",@"Localizable",@"")
#define LS_LOGIN_BUTTON NSLocalizedStringFromTable(@"login.button",@"Localizable",@"")
    EOS
    assert File.exist?("Babelish.h")
    result = File.read("Babelish.h")
    assert_equal expected_output, result
    # clean up
    system("rm -f ./Babelish.h")
  end

  def test_write_macros_process_with_special_characters
    keys = ["%d blabla", "login!", "HEY!!"]
    table = "Localizable"
    macros = Babelish::XcodeMacros.new
    macros.process(table, keys)
    macros.write_content("Babelish.h")
    expected_output = String.new(<<-EOS)
//
//  Babelish.h
//  
//  This file was generated by Babelish
//  
//  https://github.com/netbe/babelish
//
#define LS_DBLABLA NSLocalizedStringFromTable(@"%d blabla",@"Localizable",@"")
#define LS_LOGIN NSLocalizedStringFromTable(@"login!",@"Localizable",@"")
#define LS_HEY NSLocalizedStringFromTable(@"HEY!!",@"Localizable",@"")
    EOS
    assert File.exist?("Babelish.h")
    result = File.read("Babelish.h")
    assert_equal expected_output, result
    # clean up
    system("rm -f ./Babelish.h")
  end

  def test_write_macros_with_comments_process
    keys = ["login.title", "login.button"]
    table = "Localizable"
    comments = {"login.title" => "this is the login screen title", "login.button"  => nil}
    macros = Babelish::XcodeMacros.new
    macros.process(table, keys, comments)
    macros.write_content("Babelish.h")
    expected_output = String.new(<<-EOS)
//
//  Babelish.h
//  
//  This file was generated by Babelish
//  
//  https://github.com/netbe/babelish
//
#define LS_LOGIN_TITLE NSLocalizedStringFromTable(@"login.title",@"Localizable",@"this is the login screen title")
#define LS_LOGIN_BUTTON NSLocalizedStringFromTable(@"login.button",@"Localizable",@"")
    EOS
    assert File.exist?("Babelish.h")
    result = File.read("Babelish.h")
    assert_equal expected_output, result
    # clean up
    system("rm -f ./Babelish.h")
  end


end
