How do I strip off the extra double quote or white space in the string when using assert_equal method

(Ban Hoang) #1

Thanks for your help, Ben!

I am confused on your assert_equal method:

assert_equal a.gsub(/['"]/, ''), b.gsub(/['"]/, '')

Is /['"]/ will strip out the quotes when comparing?

My assert_equal method:

assert_equal(arr[0],$ie.frame("top").select_list(:name,"site_id").getAll
Contents)

1. The portion arr[0] is from the read in external file.
2. $ie.frame("top").select_list(:name,"site_id"). is from the dropdown
list application

···

=======================================

This is the csv external file dropList.csv contain:

:: Select Site :: !!Eastgate_Mall

--------------------------------------

I think my actual problem is when reading into an array and compare, it
did not separate :: Select Site :: as one field and ::
!!Eastgate_Mall to another field for double quotes

That why the expect is <[":: Select Site ::,!!Eastgate_Mall,\n"]
( missing separate double quotes)

Instead of <[":: Select Site ::", "!!Eastgate_Mall"]>.

-----------------

Thanks for your help!

-----Original Message-----
From: Ben Bleything [mailto:ben@bleything.net]
Sent: Wednesday, August 23, 2006 9:50 AM
To: ruby-talk ML
Subject: Re: How do I strip off the extra double quote or white space in
the string when using assert_equal method

On Thu, Aug 24, 2006, Ban Hoang wrote:

I try to read in an external file to array and compare them to the

drop

down list in our application. I got into the problem when comparing,

the

extra double quotes make the assertion result failed. Can anyone help

me

to make it work?

A couple of options:

1) If your external file is truly CSV (comma-separated values) use a CSV
parser to parse it. Though, from your example, it doesn't seem to be
the case... here's some code anyway.

.....
require 'csv'

rows = []

CSV.open('dropList.csv', 'r') do |row|
  rows << row
end
.....

Then compare the parsed rows against your dropdown array.

2) Write your own assertion:

.....
def assert_equal_without_quotes(a,b)
  assert_equal a.gsub(/['"]/, ''), b.gsub(/['"]/, '')
end
.....

Untested, but that might work :wink:

Ben

(Ben Bleything) #2

Thanks for your help, Ben!

I am confused on your assert_equal method:

assert_equal a.gsub(/['"]/, ''), b.gsub(/['"]/, '')

Is /['"]/ will strip out the quotes when comparing?

Notice that that call to assert_equal is wrapped inside a method. So
you would call

assert_equal_without_quotes( arr[0], $ie.frame.... )

I think the real problem is that you need to figure out how to parse
your file, though. You call it a csv but it's not. My suggestion would
be to fix it so it is :slight_smile:

I think my actual problem is when reading into an array and compare, it
did not separate :: Select Site :: as one field and ::
!!Eastgate_Mall to another field for double quotes

Correct.

That why the expect is <[":: Select Site ::,!!Eastgate_Mall,\n"]
( missing separate double quotes)

Not only is it missing the quotes, but it's adding it as a single
element instead of two, as you want.

If your file looks like this:

···

On Thu, Aug 24, 2006, Ban Hoang wrote:

-----
:: Select Site :: !!Something
:: Select Site :: !!Something else
...
-----

Then you'll need to determine how to parse that. Quick, dirty, and
untested:

-----
contents = []

File.readlines('yourfile').each do |line|
  line =~ /(:: .*? ::)\s+(!!.*?)/

  label = $1
  value = $2

  contents << [$1, $2]
end
-----

There's probably a better regex you could use, but the bottom line is,
you need to extract the values from your file instead of just grabbing
each line.

Again, it's probably easiest to just make it a real csv file :slight_smile:

Good luck!
Ben